← All languages
R function of the day
Random

str

Display the internal structure of an R object.

Description

The str() function compactly displays the internal structure of an R object. It is a diagnostic function particularly useful for understanding complex objects like nested lists, data frames, and model outputs.

str shows the type, dimensions, and a preview of the contents of each component. It is one of the most useful functions for exploring unfamiliar data structures in R.

Arguments

NameDescriptionOptional
object Any R object to inspect. No
max.level Maximum nesting level to display. Defaults to NA (all levels). Yes

Example

str(mtcars)
# 'data.frame': 32 obs. of 11 variables:
#  $ mpg : num  21 21 22.8 21.4 18.7 ...
#  $ cyl : num  6 6 4 6 8 ...
str(list(a = 1:3, b = "hello"))
# List of 2
#  $ a: int [1:3] 1 2 3
#  $ b: chr "hello"

Reference