Summary

You can now choose an R object for a task, inspect its structure, and recognize when combining or converting values changes their type.

What you learned

A data type describes values; an object structure describes how they are organized. Measurement level describes their meaning in your study. For example, a numeric station code can still represent a nominal category.

Object Structure Typical use or caution
Atomic vector A sequence of values of one type. c(1, 2, 3) stores numeric values; combining numbers and text produces character values.
Matrix Two dimensions: rows and columns. A matrix made from an atomic vector has one type throughout. By default, matrix() fills columns first.
Array Dimensions specified by dim, potentially more than two. An array made from an atomic vector also has one type throughout.
Data frame A table with equal-length columns. Ordinary atomic columns each have one type, but different columns may have different types.
List A collection of components. Components can have different types, structures, and lengths, including other lists.
Factor Categorical values with defined levels. Internally uses integer codes and level labels; ordered factors additionally record an order.
Diagram linking measurement levels to R data types and object types, and listing vectors, matrices, arrays, data frames, factors and lists. The diagram's single chain is misleading: measurement level, storage type and object structure are separate properties; discrete/continuous status does not determine measurement level. It also labels numeric, NA and NULL as data types, which they are not.
A legacy overview of measurement levels, R storage types and object structures. Its single chain oversimplifies their relationship; numeric is a class (usually integer or double storage), while NA marks a missing value and NULL is the null object. Use the distinctions and examples above.
Diagram key labels blue as Numerical, green as Character and purple as Logical. A vector and a matrix contain one color throughout; the data frame has differently colored columns, and the list contains irregular components in multiple colors. This illustrates one type per atomic vector or matrix, possibly different types by data-frame column, and mixed components in a list. Arrays and factors are not shown.
Object structures compared: atomic vectors and matrices use one type throughout; data-frame columns may have different types; lists can contain components with different types and structures. The colors are examples, and the graphic does not show every object type (for example, arrays and factors). In R, numeric values are stored as integer or double.

str() gives a compact overview. Use class() for an object’s class, typeof() for its internal type, length() for its length, and dim(), nrow(), or ncol() for dimensions where applicable. For a data frame, length() counts columns, not rows.

A short recap in R

observations <- data.frame(
  site = c("A", "B", "C", "D"),
  height_cm = c(12.5, 18, 15, 9),
  habitat = factor(c("forest", "grassland", "forest", "grassland")),
  stringsAsFactors = FALSE
)
str(observations)
dim(observations)
length(observations)

The data frame has four rows and three columns: dim(observations) returns 4 3, while length(observations) returns 3. site is character, height_cm is numeric, and habitat is a factor with two levels.

Before converting

Conversions can change meaning or lose information. Combining numbers and text in an atomic vector converts the numbers to text. Converting a mixed data frame to a matrix can do the same. as.numeric() applied directly to a factor returns its internal codes, not numeric values represented by its labels. Inspect the object and the result before continuing.

Check your understanding

  1. Which object would hold numeric measurements, site names, and habitat categories in one table?
  2. Which object would hold that table together with a separate description and a vector of a different length?
  3. Predict the type of c(1, "two"). Why is a factor’s integer code different from its category label?

Revisit object types and converting objects if needed.

Test

Updated: