# Datetime
my_datetime <- Sys.time()
my_datetime
## [1] "2025-09-19 13:07:30 EDT"
class(my_datetime)
## [1] "POSIXct" "POSIXt"
# Character
# Convert car row names into characters
car_names <- rownames(mtcars)
head(car_names)
## [1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
## [4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
class(car_names)
## [1] "character"
# Numeric
# Select numeric column (e.g. mpg)
mpg_values <- mtcars$mpg
head(mpg_values)
## [1] 21.0 21.0 22.8 21.4 18.7 18.1
class(mpg_values)
## [1] "numeric"
# Boolean
# Is mpg is greater than 15?
mpg_high <- mtcars$mpg > 15
head(mpg_high)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE
class(mpg_high)
## [1] "logical"
# Array
# Convert subset of mtcars to array
mtcars_array <- array(data = as.matrix(mtcars[1:6, 1:3]), dim = c(2, 3, 3))
mtcars_array
## , , 1
##
## [,1] [,2] [,3]
## [1,] 21 22.8 18.7
## [2,] 21 21.4 18.1
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 6 4 8
## [2,] 6 6 6
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 160 108 360
## [2,] 160 258 225
class(mtcars_array)
## [1] "array"
# Vector
# A single numeric vector from mtcars
hp_vector <- mtcars$hp
head(hp_vector)
## [1] 110 110 93 110 175 105
class(hp_vector)
## [1] "numeric"
#Dataframe (mtcars is already one)
mtcars_df <- mtcars[1:5, ]
mtcars_df
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
class(mtcars_df)
## [1] "data.frame"
# List
# A list with different types from mtcars
mtcars_list <- list(
name = rownames(mtcars)[1],
mpg = mtcars$mpg[1],
high_mpg = mtcars$mpg[1] > 20
)
mtcars_list
## $name
## [1] "Mazda RX4"
##
## $mpg
## [1] 21
##
## $high_mpg
## [1] TRUE
class(mtcars_list)
## [1] "list"
# Tibble
library(tibble)
mtcars_tibble <- as_tibble(mtcars)
head(mtcars_tibble)
## # A tibble: 6 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
class(mtcars_tibble)
## [1] "tbl_df" "tbl" "data.frame"