Package: base


Function: class()


1. Review the class for each variable

Review the data (d1)

# A tibble: 3 x 5
  Var1   Var2  Var3 Var4       Var5 
  <fct> <int> <dbl> <date>     <lgl>
1 a         2   3.6 2004-10-10 TRUE 
2 b        NA   8.5 2007-12-14 FALSE
3 c         3  NA   2020-08-09 TRUE 

View the class for each variable

class(d1$Var1)
[1] "factor"
class(d1$Var2)
[1] "integer"
class(d1$Var3)
[1] "numeric"
class(d1$Var4)
[1] "Date"
class(d1$Var5)
[1] "logical"

You could also view the class for each variable using a function like purrr::map() to iterate the base::class() function over each variable

  • Note: purrr::map() returns a list.
purrr::map(d1, class)
$Var1
[1] "factor"

$Var2
[1] "integer"

$Var3
[1] "numeric"

$Var4
[1] "Date"

$Var5
[1] "logical"

Function: typeof()


1. Review the data type for each variable

Review the data (d1)

# A tibble: 3 x 5
  Var1   Var2  Var3 Var4       Var5 
  <fct> <int> <dbl> <date>     <lgl>
1 a         2   3.6 2004-10-10 TRUE 
2 b        NA   8.5 2007-12-14 FALSE
3 c         3  NA   2020-08-09 TRUE 

View the data type for each variable

purrr::map(d1, typeof)
$Var1
[1] "integer"

$Var2
[1] "integer"

$Var3
[1] "double"

$Var4
[1] "double"

$Var5
[1] "logical"
  • Note: You will notice that our date variable (Var4) returns a data type of double (or numeric). This is because internally, R stores dates as numeric values. The class of Var4 remains date though.

  • Note: You will also notice that the data type for our factor variable (Var1) is integer even though the values appear to be character. This is because internally, R stores factor values as integers in order to create ordered levels. To learn more about your factor levels, you can use another function called base::attributes().

attributes(d1$Var1)
$levels
[1] "a" "b" "c"

$class
[1] "factor"

Package: utils


Function: str()


1. Review the structure of the data frame/tibble

Review the data (d1)

# A tibble: 3 x 5
  Var1   Var2  Var3 Var4       Var5 
  <fct> <int> <dbl> <date>     <lgl>
1 a         2   3.6 2004-10-10 TRUE 
2 b        NA   8.5 2007-12-14 FALSE
3 c         3  NA   2020-08-09 TRUE 

View the structure of the data frame/tibble as well as the class of each variable

utils::str(d1)
tibble [3 x 5] (S3: tbl_df/tbl/data.frame)
 $ Var1: Factor w/ 3 levels "a","b","c": 1 2 3
 $ Var2: int [1:3] 2 NA 3
 $ Var3: num [1:3] 3.6 8.5 NA
 $ Var4: Date[1:3], format: "2004-10-10" "2007-12-14" ...
 $ Var5: logi [1:3] TRUE FALSE TRUE

Package: dplyr


Function: glimpse()


1. Review the structure of the data frame/tibble

Review the data (d1)

# A tibble: 3 x 5
  Var1   Var2  Var3 Var4       Var5 
  <fct> <int> <dbl> <date>     <lgl>
1 a         2   3.6 2004-10-10 TRUE 
2 b        NA   8.5 2007-12-14 FALSE
3 c         3  NA   2020-08-09 TRUE 

View the structure of the data frame/tibble as well as the class of each variable

dplyr::glimpse(d1)
Rows: 3
Columns: 5
$ Var1 <fct> a, b, c
$ Var2 <int> 2, NA, 3
$ Var3 <dbl> 3.6, 8.5, NA
$ Var4 <date> 2004-10-10, 2007-12-14, 2020-08-09
$ Var5 <lgl> TRUE, FALSE, TRUE

Return to Data Types