Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mutate and ifelse in a loop?

What I do is to create dummies to indicate whether a continuous variable exceeds a certain threshold (1) or is below this threshold (0). I achieved this via several repetitive mutates, which I would like to substitute with a loop.

# load tidyverse
library(tidyverse)

# create data
data <- data.frame(x = runif(1:100, min=0, max=100))

# What I do
data <- data %>%
  mutate(x20 = ifelse(x >= 20, 1, 0)) %>%
  mutate(x40 = ifelse(x >= 40, 1, 0)) %>%
  mutate(x60 = ifelse(x >= 60, 1, 0)) %>%
  mutate(x80 = ifelse(x >= 80, 1, 0)) 
  
# What I would like to do
for (i in seq(from=0, to=100, by=20)){
  data %>% mutate(paste(x,i) = ifelse(x >= i, 1,0))
}

Thank you.

like image 547
Marco Avatar asked Dec 18 '22 12:12

Marco


1 Answers

You can use map_dfc here :

library(dplyr)
library(purrr)
breaks <- seq(from=0, to=100, by=20)
bind_cols(data, map_dfc(breaks, ~
          data %>% transmute(!!paste0('x', .x) := as.integer(x > .x))))

#             x x0 x20 x40 x60 x80 x100
#1    6.2772517  1   0   0   0   0    0
#2   16.3520358  1   0   0   0   0    0
#3   25.8958212  1   1   0   0   0    0
#4   78.9354970  1   1   1   1   0    0
#5   35.7731737  1   1   0   0   0    0
#6    5.7395139  1   0   0   0   0    0
#7   49.7069551  1   1   1   0   0    0
#8   53.5134559  1   1   1   0   0    0
#...
#....     

Although, I think it is much simpler in base R :

data[paste0('x', breaks)] <- lapply(breaks, function(x) as.integer(data$x > x))
like image 87
Ronak Shah Avatar answered Dec 26 '22 18:12

Ronak Shah