Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block 2, Task 3: Three R Ecosystems to Manipulate Data #7

Open
mbannert opened this issue Oct 21, 2020 · 0 comments
Open

Block 2, Task 3: Three R Ecosystems to Manipulate Data #7

mbannert opened this issue Oct 21, 2020 · 0 comments
Labels
task student task / exercise

Comments

@mbannert
Copy link
Contributor

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
# dplyr aka tidyverse
library(dplyr)
mtcars %>% 
  mutate(eco = ifelse(mpg > 22, TRUE, FALSE))

mtcars
@mbannert mbannert added the task student task / exercise label Oct 21, 2020
@mbannert mbannert changed the title Block 2, Task 2: Three R Ecosystems to Manipulate Data Block 2, Task 3: Three R Ecosystems to Manipulate Data Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
task student task / exercise
Projects
None yet
Development

No branches or pull requests

1 participant