The str() function in R is used to display internal structure of an R object.

The following method shows how you can do it with syntax.

Method 1: Use str() Function

str(object)

The following example shows how to use str() function in R.

Apply str() Function on Vector

Let’s see how we can check internal structure of an array using str() function:

# Create vector
x=c(78,89,85,84,81,79,77,85,77,78,75,74,71,79,76,78)

# Show vector
print(x)

# Display internal structure of vector
str(x)

Output:

[1] 78 89 85 84 81 79 77 85 77 78 75 74 71 79 76 78

num [1:16] 78 89 85 84 81 79 77 85 77 78 ...

As the output shows x is numeric vector with 16 elements in it.

Apply str() Function on Data Frame

The below example shows how we can apply str() function on dataframe:

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H"),
                 Pressure=c(12.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58),
                 Temperature=c(78,89,85,84,81,79,77,85),
                 Humidity=c(5,7,1,2,7,8,9,4),
                 Status=c("OK","Suspect","OK","OK","Suspect","Suspect","OK","Suspect"),
                 Started=c("Yes","No","Yes","Yes","No","No","Yes","No"))

# Show dataframe
print(df)

# Show datatype
str(df)

Output:

  Machine_name Pressure Temperature Humidity  Status Started
1            A    12.39          78        5      OK     Yes
2            B    11.25          89        7 Suspect      No
3            C    12.15          85        1      OK     Yes
4            D    13.48          84        2      OK     Yes
5            E    13.78          81        7 Suspect      No
6            F    12.89          79        8 Suspect      No
7            G    12.21          77        9      OK     Yes
8            H    12.58          85        4 Suspect      No

'data.frame':	8 obs. of  6 variables:
 $ Machine_name: chr  "A" "B" "C" "D" ...
 $ Pressure    : num  12.4 11.2 12.2 13.5 13.8 ...
 $ Temperature : num  78 89 85 84 81 79 77 85
 $ Humidity    : num  5 7 1 2 7 8 9 4
 $ Status      : chr  "OK" "Suspect" "OK" "OK" ...
 $ Started     : chr  "Yes" "No" "Yes" "Yes" ...

The output indicates that datatype of each column of dataframe also it gives total count of columns and rows.

The str() function basically used to get length, datatype, class and elements of object.