Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
The str() function in R prints a compact summary of the internal structure of any R object: its type, its length or dimensions, and the first few values of each component. The R manual describes it as “a diagnostic function and an alternative to summary,” and says that ideally only one line is displayed for each basic structure. Use it right after loading data, before you write any analysis code.
This guide covers the syntax, the arguments that control how much is shown (max.level, vec.len, list.len, give.attr), how str() differs from glimpse() and summary(), and what to do when the output looks cut off.
What str() is used for in research
Most analysis errors in R trace back to an object that is not what the analyst assumed: a numeric column read in as text, a list where a vector was expected, a data frame with an unexpected number of columns. str() answers “what is this object, and what is inside it?” in one call, so it is the usual first check after importing a dataset (see data frames in R) and before modelling.
- Check types after import. Confirm that each variable has the type your analysis plan assumes (see R data types).
- Explore nested objects. Lists, model objects and results from other packages are often several levels deep;
str()shows how they are organised so you can extract the part you need. - Document the data you started from. Pasting the output into a script comment or a lab notebook records the shape and types of a dataset at the time of analysis.
Install or access
str() is documented in the R manual as part of the utils package, so it needs no separate installation: it is available once R itself is installed. R is obtained from the Comprehensive R Archive Network (CRAN). If you work in an editor, RStudio is a common choice. The alternative glimpse() comes from the pillar package and is re-exported by dplyr; see how to install R packages to add it.
Step-by-step: inspect an object with str()
- Call str() on the object. The generic form is
str(object, ...). For a built-in example,str(1:12)is one of the examples in the R manual. For your own data:str(my_data). - Read the output line by line. Each component of the object gets its own line (see the next sections for how to read one).
- Limit nesting for lists. Set
max.levelto stop at a given depth:str(my_list, max.level = 1)shows only the top-level components. The default,NA, descends through all levels. - Show more or fewer values.
vec.lensets how many leading elements of each vector appear (default 4):str(my_data, vec.len = 8). - Do not assign the result. The manual states that “str does not return anything, for efficiency reasons,” so
x <- str(obj)stores nothing useful. Call it for its printed output only.
Arguments that control the output
These defaults come from the str help page in the R manual (R-devel documentation, checked 2026-10-29). The defaults for several arguments are set by strOptions().
| Argument | Default | What it controls |
|---|---|---|
max.level |
NA (all levels) |
Maximum depth of nested components shown |
vec.len |
4 |
Numeric guide for how many leading elements of each vector are displayed |
list.len |
99 |
Maximum number of list elements shown at each level |
give.attr |
TRUE |
Whether attributes are shown |
digits.d |
3 |
Number of significant digits for numeric values |
nchar.max |
128 |
Maximum number of characters shown for a character string |
Output and how to read it
Each component is printed on its own line, prefixed with the component marker ($ by default, set by comp.str). Nested lists appear indented beneath their parent, so indentation tells you the depth.
For comparison, the dplyr documentation shows what glimpse(mtcars) prints: a header of Rows: 32 and Columns: 11, then one line per column such as $ mpg <dbl> 21.0, 21.0, 22.8 .... Both functions follow the same one-line-per-column idea; they differ in layout and in what they return, as the next sections show.
Troubleshooting
The output is cut off after a few values
That is the vec.len default of 4 at work. Raise it, for example str(x, vec.len = 10). For long character strings, nchar.max (default 128) caps the characters shown.
A long list shows only part of its elements
list.len defaults to 99 elements per level. Increase it if you need more, or use max.level = 1 to see the top-level components only.
There is too much attribute noise
Set give.attr = FALSE to hide attributes and keep the structure lines readable.
Assigning the result gives nothing
The function returns nothing by design. If you want an overview step inside a pipeline, use glimpse(), which returns its input invisibly.
Lines wrap or run off the screen
Width follows getOption("width") by default, and strict.width accepts "no", "cut" or "wrap" to control how long lines are handled.
Free alternatives: glimpse() and summary()
| Function | Package | What the documentation says |
|---|---|---|
str() |
utils | Compact structure of any object, one line per basic structure; returns nothing (R manual, str) |
glimpse() |
pillar, re-exported by dplyr | Like a transposed print(): columns run down the page, data runs across; tries to show as much data as possible; always shows the underlying data, even for a remote data source; returns its input invisibly so it can sit inside a pipeline (dplyr reference, glimpse) |
summary() |
see the R manual | The function the R manual names as the one str() is an alternative to (R manual, str) |
The dplyr documentation describes glimpse() as “a little like str() applied to a data frame”. In a pipeline, mtcars |> glimpse() |> select(1:3) prints the overview and then continues with the original data.
FAQ
What does str() do in R?
It displays the internal structure of an R object compactly, showing the type, size and leading values of each component, without changing the object.
What is the difference between str() and glimpse()?
glimpse() is laid out like a transposed print() and tries to show as much data as possible; str() returns nothing. glimpse() returns its input invisibly, so it fits in a pipe.
How do I see only the top level of a nested list?
Use str(x, max.level = 1).
Why does str() not show all my values?
By default only a few leading elements of each vector are shown (vec.len is 4). Raise vec.len to show more.
Is str() in base R?
The R manual documents it in the utils package, which is the package named in its help page header.
Sources
- R manual,
str(package utils), R-devel documentation: stat.ethz.ch/R-manual/R-devel/library/utils/html/str.html, checked 2026-10-29. - dplyr reference,
glimpse: dplyr.tidyverse.org/reference/glimpse.html, checked 2026-10-29.
Related reading: what a vector is in R and select() in R.







