This exercise lets you experience the different behavior of different ways to handle data.
Run the following blocks of code separately. First run the baseR and discuss within your group what happens.
Analyze and evaluate different approaches.
# base R
basecars <- mtcars
basecars$eco <- FALSE
basecars[basecars$mpg > 22,"eco"] <- TRUE
basecars
# data.table
library(data.table)
dtcars <- as.data.table(mtcars)
dtcars[, eco := ifelse(mpg > 22, TRUE, FALSE)]
dtcars
# Bonus
# change the function and explore the
# effect of copy()
dtcars_2 <- as.data.table(mtcars)
f <- function(d){
d[, eco := ifelse(mpg > 22, TRUE, FALSE)]
message("Object modified in place")
}
f(dtcars_2)
dtcars_2
This exercise lets you experience the different behavior of different ways to handle data. Run the following blocks of code separately. First run the baseR and discuss within your group what happens.
Analyze and evaluate different approaches.