str() (R)
str() compactly displays the structure of any R object — its type, dimensions, and a preview of its contents. It is the first command analysts run to understand unfamiliar data.
R
str() (short for 'structure') prints a dense, one-screen summary of an object: for a data frame it shows the number of observations and variables, each column's type, and the first few values; for a list it shows the nested structure. This makes it the go-to diagnostic after importing or building data.
It's especially valuable for spotting type problems — a numeric column read as character, or a variable that became a factor when you expected text — before they cause errors downstream. It works on anything: vectors, lists, models, and complex nested objects.
str() complements other inspection tools: head()/tail() show example rows, summary() gives per-column statistics, names()/dim()/class() report specific attributes, and glimpse() (dplyr) is a tidyverse cousin. Reaching for str() first is a core habit of fluent R users.
Syntax
str(object)Examples
R
df <- data.frame(
id = 1:2,
name = c("Ana", "Sam"),
active = c(TRUE, FALSE)
)
str(df)Output:
'data.frame': 2 obs. of 3 variables:
$ id : int 1 2
$ name : chr "Ana" "Sam"
$ active: logi TRUE FALSE