R language consists of different data types and each data type has its own 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() Function in R
To display the internal data structure of an R object, use the str() function. The str() is a built-in R function that can show the internal structure of large lists that are nested. The str() function is used as an alternative to summary() function but the str() method is more compact than summary() method.
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.
The str() function takes an R Object whose internal information we required.
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) rv str(rv)
 11 18 19 21 46 num [1:5] 11 18 19 21 46
As you can see that we defined a numeric vector means the vector consisted of numbers.
str() function on R List
To see the internal data structure of the list, use the str() function in the R language. To define a list, use the list() function and pass the elements as arguments.
rl <- list(11, 18, 19, 21) str(rl)
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, which is saying that the list contains four elements with its data type.
str() function on R Matrix
To see 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) mtrx cat("The structure of matrix is", "\n") str(mtrx)
[,1] [,2] [1,] 11 19 [2,] 18 21 The structure of matrix is num [1:2, 1:2] 11 18 19 21
str() function on R 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)) arr cat("The structure of array is", "\n") str(arr)
, , 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
str() function on R data frame
To see the internal structure of the data frame in R, use the str() function. To create a data frame in R, 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 ) str(streaming)
'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 the information that 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.