Replacing subsets in R with Tidyverse

How to replace subset values ​​in R with Tidyverse ?

Using the cars data as an example, if I wanted to change all speed below 30 to 0, I can use the following command:

  cars[cars["speed"] < 30,] <- 0 

Using Tidyverse you can generate the same subset with more readable commands:

  cars %>% filter(speed < 30) %>% mutate(speed =0) 

However, this changes the subset of the data that we extracted from cars , and not the values ​​of the observations within cars .

I might have missed something obvious, but is there an intuitive way to do the same with Tidyverse ? Although cars[cars["speed"] < 30,] <- 0 works fine in most cases, it becomes really cumbersome if it has more than 5 conditions to satisfy.

+5
source share
1 answer

You can use the replace function:

 cars %>% mutate(speed = replace(speed, speed < 30, 0)) 

The ifelse condition will also work:

 cars %>% mutate(speed = ifelse(speed < 30, 0, speed)) 

I tested this on a data frame of a million rows and replace did approximately one-eighth of the ifelse time.

 library(microbenchmark) set.seed(2) dat = data.frame(x=runif(1e6, 0, 1000), y=runif(1e6, 0, 1000)) microbenchmark( replace=dat %>% mutate(x=replace(x, x<200, 0)), ifelse=dat %>% mutate(x=ifelse(x<200, 0, x)), if_else=dat %>% mutate(x=if_else(x<200, 0, x)), times=100 ) 
 Unit: milliseconds expr min lq mean median uq max neval cld replace 8.352943 9.55682 18.16755 11.45507 15.33215 224.8759 100 a ifelse 71.782371 87.37754 165.95928 95.12722 262.73016 287.3633 100 c if_else 39.947845 47.83934 88.72291 51.99449 59.76760 251.0381 100 b 
+7
source

Source: https://habr.com/ru/post/1266563/


All Articles