str()
get_help()
docs
The str()
function comes with R
and is part of the Base R
{utils}
package.
We use this function to quickly view the structure of a given variable, commonly a tibble (data frame). When used on a data frame, this function will show the number rows and columns, column names, columns types, and information about the top values.
Note that there is related function in the {dplyr}
package called dplyr::glimpse()
which offers an improved view of the structure of tibbles (data frames).
str(a data frame or other variable to see structure of)
Some examples below use the carnivores
dataset. Learn more about this dataset with get_help("carnivores")
.
# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445
## 2 Cheetah Acinonyx 11.9 NA
## 3 Dog Canis 13.9 0.07
## 4 Gray seal Haliochoerus 17.8 0.325
## 5 Jaguar Panthera 13.6 0.157
## 6 Lion Panthera 10.5 NA
## 7 Northern fur seal Callorhinus 15.3 NA
## 8 Red fox Vulpes 14.2 0.0504
## 9 Tiger Panthera 8.2 NA
# Show structure of the carnivores tibble
str(carnivores)
## tibble [9 × 4] (S3: tbl_df/tbl/data.frame)
## $ name : chr [1:9] "Arctic fox" "Cheetah" "Dog" "Gray seal" ...
## $ genus : Factor w/ 6 levels "Acinonyx","Callorhinus",..: 6 1 3 4 5 5 2 6 5
## $ awake : num [1:9] 11.5 11.9 13.9 17.8 13.6 10.5 15.3 14.2 8.2
## $ brainwt: num [1:9] 0.0445 NA 0.07 0.325 0.157 NA NA 0.0504 NA
# Show structure of the iris data frame, which comes built in with R
# Note how the output shows this is technically a data frame, not a tibble
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...