The str() function in R is “used to display the internal structure of any R object compactly”. For example, the str(df) function returns information about the rows, columns, names, classes, and some observations of df.
Syntax
str(object)
Parameters
object: It takes an R Object whose internal information we require.
Example 1: Use the str() function with Vector
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)
Output
[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.
Example 2: Use the str() function with 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)
str(rl)
Output
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.
Example 3: Using the 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)
mtrx
cat("The structure of matrix is", "\n")
str(mtrx)
Output
[,1] [,2]
[1,] 11 19
[2,] 18 21
The structure of matrix is
num [1:2, 1:2] 11 18 19 21
Example 4: Use the str() function with an 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)
Output
, , 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
Example 5: Using str() function with 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
)
str(streaming)
Output
'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 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 the last is service_price.
That’s it.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.