Home » How to Use str() Function in R (4 Examples)

How to Use str() Function in R (4 Examples)

by Erma Khan
spot_img

You can use the str() function in R to display the internal structure of any R object in a compact way.

This function uses the following basic syntax:

str(object)

where:

  • x: The name of the object to display the structure for

The following examples show how to use this function in different scenarios.

Example 1: Use str() with Vector

The following code shows how to use the str() function to display the internal structure of a vector in a compact way:

#create vector
x #display internal structure of vector
str(x)

num [1:12] 2 4 4 5 8 10 NA 15 12 12 ...

From the output we can see:

  • The vector has a class of numeric
  • The vector has a length of 12

By default, the str() function also displays the first 10 items in the vector.

Example 2: Use str() with Data Frame

The following code shows how to use the str() function to display the internal structure of a data frame in a compact way:

#create data frame
df frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))	

#display internal structure of data frame
str(df)

'data.frame':	5 obs. of  4 variables:
 $ team    : chr  "A" "B" "C" "D" ...
 $ points  : num  99 90 86 88 95
 $ assists : num  33 28 31 39 34
 $ rebounds: num  30 28 24 24 28

From the output we can see:

  • The object has a class of data.frame
  • The data frame has 5 observations (rows) and 4 variables (columns)

The name of each variable in the data frame is also shown along with the class of each variable and the first few values.

Using the str() function is an excellent way to gain a quick understanding of a data frame, especially if the data frame is very large.

In practice, the str() function is one of the first functions used after loading a data frame into R, even before performing any exploratory analysis or statistical modeling.

Example 3: Use str() with Matrix

The following code shows how to use the str() function to display the internal structure of a matrix in a compact way:

#create matrix
mat 5)

#view matrix
mat

     [,1] [,2] [,3]
[1,]    1    6   11
[2,]    2    7   12
[3,]    3    8   13
[4,]    4    9   14
[5,]    5   10   15

#display internal structure of matrix
str(mat)

 int [1:5, 1:3] 1 2 3 4 5 6 7 8 9 10 ...

From the output we can see:

  • The matrix has a class of integer
  • The matrix has 5 rows and 3 columns

By default, the str() function also displays the first 10 values in the vector.

Example 4: Use str() with List

The following code shows how to use the str() function to display the internal structure of a list in a compact way:

#create list
my_list #view list
my_list

$A
[1] 1 2 3 4 5

$B
[1] 2 9

$C
[1] "hey"   "hello"

#display internal structure of list
str(my_list)

List of 3
 $ A: int [1:5] 1 2 3 4 5
 $ B: num [1:2] 2 9
 $ C: chr [1:2] "hey" "hello"

From the output we can see:

  • The list has 3 elements
  • The first element has a name of A, a class of integer, a length of 5, and all 5 values are shown.
  • The second element has a name of B, a class of numeric, a length of 2, and the 2 values are shown.
  • The third element has a name of C, a class of character, a length of 2, and the 2 values are shown.

By just using the str() function, we’re able to gain a full understanding of the structure of the list.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Use length() Function in R
How to Use cat() Function in R
How to Use substring() Function in R

spot_img

Related Posts