str in R: How to Check Data Structure of Object in R

R language consists of different data types, and each data type has its unique ability. When we are working with large datasets, sometimes, we need to get an overview of the specific structure, and to solve that problem, we can use the str() function. It compactly displays the internal structure of an R object.

str in R

The str() in R is a built-in function that can show the internal structure of large lists that are nested. The str() method accepts R Object as an argument and returns the internal information about that object.

The str() method is used as an alternative to summary() function but the str() method is more compact than summary() method.

To display the internal data structure of an R object, use the str() function. The str() function returns information about the rows(observations) and columns(variables) along with extra information like the names of the columns, class of each column, followed by some of the initial observations of each of the columns.


str(object, …)


The str() function takes an R Object whose internal information we require.


Let’s take a vector as an R object and find its internal structure using the str() function.

rv <- c(11, 18, 19, 21, 46)


[1] 11 18 19 21 46
num [1:5] 11 18 19 21 46

As you can see that we defined a numeric vector means a vector consisting of numbers.

Applying str() function on List

To check the internal data structure of the list in R, use the str() function. To define a list, use the list() function and pass the elements as arguments.

rl <- list(11, 18, 19, 21)


List of 4
$ : num 11
$ : num 18
$ : num 19
$ : num 21

In the output, we can see that the str() function returns the information about the list, saying that the list contains four elements with its data type.

Using str() function on Matrix

To display the internal structure of a matrix in R, use the str() function. To create a matrix in R, use the matrix() function, and pass the vector, nrow, and ncol parameters.

rv <- c(11, 18, 19, 21)
mtrx <- matrix(rv, nrow = 2, ncol = 2)
cat("The structure of matrix is", "\n")


     [,1] [,2]
[1,]  11   19
[2,]  18   21
The structure of matrix is
num [1:2, 1:2] 11 18 19 21

Applying str() function on Array

To see the internal structure of an array in R, use the str() function. To create an array in R, use the array() function. The array() function takes a vector as an argument and uses the dim parameter to create an array.

rv <- c(19, 21)
rv2 <- c(46, 4)
arr <- array(c(rv, rv2), dim = c(2, 2, 2))
cat("The structure of array is", "\n")


, , 1

     [,1] [,2]
[1,]  19   46
[2,]  21   4

, , 2

     [,1] [,2]
[1,]  19   46
[2,]  21   4

The structure of array is
num [1:2, 1:2, 1:2] 19 21 46 4 19 21 46 4

Using str() function on data frame

To check the internal structure of the data frame in R, use the str() function. To create a data frame, use the data.frame() function.

streaming <- data.frame(
 service_id = c(1:5),
 service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
 service_price = c(18, 10, 15, 7, 12),
 stringsAsFactors = FALSE



'data.frame': 5 obs. of 3 variables:
$ service_id : int 1 2 3 4 5
$ service_name : chr "Netflix" "Disney+" "HBOMAX" "Hulu" ...
$ service_price: num 18 10 15 7 12

It provides us with the data frame with 5 observations(rows) of 3 variables(columns). Then it tells us about each variable one by one as follows, the first column names service_id of type integer followed by the 5 values, and the second column is named service_name, which is a type of char, and last is service_price.


The str() function in R is beneficial 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. That is it for this tutorial.

Leave a Comment