str() Function in R (3 Examples) | Compactly Display Structure of Object
In this article, I’ll illustrate how to print the structure of a data object using the str function in the R programming language.
The content of the page is structured as follows:
Let’s jump right to the tutorial!
Example 1: Display Structure of Data Frame Using str() Function
Example 1 explains how to use the str function to print information on the structure of a data frame object.
First, we have to create an exemplifying data frame in R:
data <- data.frame(x1 = 1:5, # Create example data frame x2 = letters[1:5], x3 = factor(c("yes", "no", "yes", "yes", "no"))) data # Print example data frame
As shown in Table 1, we have created a data frame object with three columns by executing the previous R programming syntax.
Let’s apply the str function to this data frame:
str(data) # Apply str to data frame # 'data.frame': 5 obs. of 3 variables: # $ x1: int 1 2 3 4 5 # $ x2: chr "a" "b" "c" "d" ... # $ x3: Factor w/ 2 levels "no","yes": 2 1 2 2 1
Have a look at the previous output of the RStudio console. It shows some information on each of the variables of our data set.
The variable x1 is an integer and contains the values 1 to 5, the variable x2 is a character string and contains letters such as “a”, “b”, “c”, and “d”, and the variable x3 is a factor and contains the two factor levels “no” and “yes”.
Example 2: Display Structure of List Using str() Function
In Example 2, I’ll illustrate how to check the structure of a list object using the str function.
Let’s create an example list:
my_list <- list(letters[3:1], # Create example list 555, c(1, 3, 5)) my_list # Print example list # [] #  "c" "b" "a" # # [] #  555 # # [] #  1 3 5
Next, we can apply the str function to our list as shown below:
str(my_list) # Apply str to list # List of 3 # $ : chr [1:3] "c" "b" "a" # $ : num 555 # $ : num [1:3] 1 3 5
The RStudio console output shows that our list has three list elements. The first list element is a character string containing the letters “c”, “b”, and “a”, the second list element is the numeric value 555, and the third list element is a numeric vector containing the values 1, 3, and 5.
Example 3: Display Structure of Vector Using str() Function
The following syntax shows how to use the str command to evaluate the structure of a vector object.
For this, we first have to create a vector in R:
vec <- c(1, 5, 3, 7, 8, 7, 7, 3) # Create example vector vec # Print example vector #  1 5 3 7 8 7 7 3
Next, we can apply the str function to this vector:
str(vec) # Apply str to vector # num [1:8] 1 5 3 7 8 7 7 3
The previous output illustrates that our vector is numeric and ranges from 1 to 8.
Video & Further Resources
Do you need further info on the R programming syntax of this page? Then you could have a look at the following video on my YouTube channel. I’m explaining the R programming syntax of this page in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might want to read the related tutorials which I have published on statisticsglobe.com. You can find some tutorials below:
- summary Function in R
- Compute Summary Statistics by Group
- Important Functions in R (Programming Examples)
- All R Programming Tutorials
This article has illustrated how to apply the str function to compactly display the structure of a data object in the R programming language. If you have further questions, don’t hesitate to tell me about it in the comments section.
Statistics Globe Newsletter