How can I use the str() function in R?

How can I use the str() function in R?

The str() function in R is used to display the structure of any object in the R programming language. It provides information such as the type of object, the number of elements, and the data type of each element. This function is useful for understanding the composition of an object and can aid in debugging and troubleshooting code. To use the str() function, simply pass the object as an argument and the function will return a detailed description of its structure. This can be particularly helpful when working with complex or large datasets in R.

Use str() Function in R (4 Examples)


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 <- c(2, 4, 4, 5, 8, 10, NA, 15, 12, 12, 19, 24)

#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 <- data.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)

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 <- matrix(1:15, nrow=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 <- list(A=1:5, B=c(2, 9), C=c('hey', 'hello'))

#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

Cite this article

stats writer (2024). How can I use the str() function in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-str-function-in-r/

stats writer. "How can I use the str() function in R?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-str-function-in-r/.

stats writer. "How can I use the str() function in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-str-function-in-r/.

stats writer (2024) 'How can I use the str() function in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-str-function-in-r/.

[1] stats writer, "How can I use the str() function in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use the str() function in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top