Open In App

Display the internal Structure of an Object in R Programming – str() Function

Improve
Improve
Like Article
Like
Save
Share
Report

str() function in R Language is used for compactly displaying the internal structure of a R object. It can display even the internal structure of large lists which are nested. It provides one liner output for the basic R objects letting the user know about the object and its constituents. It can be used as an alternative to summary() but str() is more compact than summary(). It gives information about the rows(observations) and columns(variables) along with additional information like the names of the columns, class of each columns followed by few of the initial observations of each of the columns.

Syntax: str(object, …)

Parameter:
object: Any R object about which information is required.

Example 1:




# R program to display 
# structure of a list
  
# Creating a list
gfg <- list(2, 4, 5, 6, 7, 9, 13, 15, 3, 1)
  
# Calling str() function
str(gfg)


Output:

List of 10
 $ : num 2
 $ : num 4
 $ : num 5
 $ : num 6
 $ : num 7
 $ : num 9
 $ : num 13
 $ : num 15
 $ : num 3
 $ : num 1

Here, we can observe the output which is a description of the object gfg. It mentions that it is a list having 10 components. In the following rows, it displays each one of them along with their class i.e. numeric in this case.

Example 2:




# R program to display structure 
# of a pre-defined dataset
  
# Importing Library
library(datasets)
  
# Importing dataset
head(airquality)
  
# Calling str() function
str(airquality)


Here, head(airquality) will display the first few rows of the data frame. After executing, the following output will be displayed.
Output :

  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6
'data.frame':    153 obs. of  6 variables:
 $ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...
 $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
 $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
 $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
 $ Month  : int  5 5 5 5 5 5 5 5 5 5 ...
 $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...

It provides us the information that the dataset airquality is a data frame with 153 observations(rows) of 6 variables(columns). Then it tells us about each variable one by one as follows, the first column with name Ozone is of type integer followed by few of its values and the second column is named Solar.R which is also of the integer type followed by few of its contents and so on.

str() will be really useful when we are unsure about the contents of an object as it will help us take a quick preview of the contents and structure of the object. This will also help in revealing issues in the naming of the columns, class of the content, etc, if any exist.



Last Updated : 05 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads