Coercion: Taming your data
It is possible to transform your data from one type to the other. Next to the class()
function, you can use the as.*()
functions to enforce data to change types. For example,
var <- "3"
var_num <- as.numeric(var)
converts the character string "3"
in var
to a numeric 3
and assigns it to var_num
. However, keep in my that it is not always possible to convert the types without losing information or getting errors.
as.integer("4.5")
as.numeric("three")
The first line will convert the character string "4.5"
to the integer 4
. The second one will convert the character string "three"
to an NA
.
This exercise is part of the course
Inferential Statistics
Exercise instructions
-
var1
is logical. Convert it to character and assign it to the variablevar1_char
. - Next, see whether
var1_char
actually is a character by using theis.character()
function on it. -
var2
is numeric. Convert it logical and assign it to the variablevar2_log
. - Inspect the class of
var2_log
usingclass()
. - Finally,
var3
is of type character. Convert it to numeric and assign the result tovar3_num
. Was it successful?
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create variables var1, var2 and var3
var1 <- TRUE
var2 <- 0.3
var3 <- "i"
# var1 is logical; convert it to character: var1_char
# See whether var1_char is a character
# Var2 is numeric; convert it to logical: var2_log
# Inspect the class of var2_log
# var3 is character; convert it to numeric: var3_num