How to check the data frame structure without using str function in R?


To check the data frame structure without using str function in R, we can follow the below steps −

  • First of all, load the data or create new data or use an in-built data set.
  • Then, use glimpse function of tibble package.

Example 1

Use in-built data set

Consider the mtcars data set, load the tibble package and use glimpse function to look at the structure of mtcars data −

library(tibble)
glimpse(mtcars)

Output

Rows: 32
Columns: 11
$ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,~
$ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,~
$ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16~
$ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180~
$ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,~
$ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.~
$ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18~
$ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,~
$ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,~
$ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,~
$ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,~

Example 2

Use in-built data set

Consider the CO2 data set, load the tibble package and use glimpse function to look at the structure of CO2 data −

library(tibble)
glimpse(CO2)

Output

Rows: 84
Columns: 5
$ Plant     <ord> Qn1, Qn1, Qn1, Qn1, Qn1, Qn1, Qn1, Qn2, Qn2, Qn2, Qn2, Qn2, ~
$ Type      <fct> Quebec, Quebec, Quebec, Quebec, Quebec, Quebec, Quebec, Queb~
$ Treatment <fct> nonchilled, nonchilled, nonchilled, nonchilled, nonchilled, ~
$ conc      <dbl> 95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 500, 6~
$ uptake    <dbl> 16.0, 30.4, 34.8, 37.2, 35.3, 39.2, 39.7, 13.6, 27.3, 37.1, ~

Example 3

Use in-built data set

Consider the iris data set, load the tibble package and use glimpse function to look at the structure of iris data −

library(tibble)
glimpse((iris)

Output

Rows: 150
Columns: 5
$ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.~
$ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.~
$ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.~
$ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.~
$ Species      <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s~

Example 4

Use in-built data set

Consider the sleep data set, load the tibble package and use glimpse function to look at the structure of sleep data −

library(tibble)
glimpse(sleep)

Output

Rows: 20
Columns: 3
$ extra <dbl> 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0, 1.9, 0.8, ~
$ group <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
$ ID    <fct> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Example 5

Use in-built data set

Consider the ChickWeight data set, load the tibble package and use glimpse function to look at the structure of ChickWeight data −

library(tibble)
glimpse((ChickWeight)

Output

Rows: 578
Columns: 4
$ weight <dbl> 42, 51, 59, 64, 76, 93, 106, 125, 149, 171, 199, 205, 40, 49, 5~
$ Time   <dbl> 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 1~
$ Chick  <ord> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, ~
$ Diet   <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~

Example 6

Use in-built data set

Consider the DNase data set, load the tibble package and use glimpse function to look at the structure of DNase data −

library(tibble)
glimpse((DNase)

Output

Rows: 176
Columns: 3
$ Run     <ord> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,~
$ conc    <dbl> 0.04882812, 0.04882812, 0.19531250, 0.19531250, 0.39062500, 0.~
$ density <dbl> 0.017, 0.018, 0.121, 0.124, 0.206, 0.215, 0.377, 0.374, 0.614,~

Example 7

Use in-built data set

Consider the Nile data set, load the tibble package and use glimpse function to look at the structure of Nile data −

library(tibble)
glimpse(Nile)

Output

Time-Series [1:100] from 1871 to 1970: 1120 1160 963 1210 1160 1160 813 1230 1370
1140 …

Example 8

Use in-built data set

Consider the HairEyeColor data set, load the tibble package and use glimpse function to look at the structure of HairEyeColor data −

library(tibble)
glimpse(HairEyeColor)

Output

‘table’ num [1:4, 1:4, 1:2] 32 53 10 3 11 50 10 30 10 25 …
- attr(*, “dimnames”)=List of 3
..$ Hair: chr [1:4] “Black” “Brown” “Red” “Blond”
..$ Eye : chr [1:4] “Brown” “Blue” “Hazel” “Green”
..$ Sex : chr [1:2] “Male” “Female”

Example 9

Use in-built data set

Consider the Indometh data set, load the tibble package and use glimpse function to look at the structure of Indometh data −

library(tibble)
glimpse(Indometh)

Output

Rows: 66
Columns: 3
$ Subject <ord> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,~
$ time    <dbl> 0.25, 0.50, 0.75, 1.00, 1.25, 2.00, 3.00, 4.00, 5.00, 6.00, 8.~
$ conc    <dbl> 1.50, 0.94, 0.78, 0.48, 0.37, 0.19, 0.12, 0.11, 0.08, 0.07, 0.~

Example 10

Use in-built data set

Consider the AirPassengers data set, load the tibble package and use glimpse function to look at the structure of AirPassengers data −

library(tibble)
glimpse(AirPassengers)

Output

Time-Series [1:144] from 1949 to 1961: 112 118 132 129 121 135 148 148 136 119 ...

Updated on: 11-Aug-2021

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements