Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Looping creating new variable based on case when conditional

$
0
0

I'm new to R and trying to build a loop where create a new variable based on a case when conditional.

for(i in 2:10){

  variable_1 <- paste0("a_", i)

  variable_2 <- paste0("b_", i)

  variable_3 <- paste0("c_", i)

  data1 <- data1 %>%
  mutate_(variable_3 = case_when(is.na(variable_1) & !is.na(variable_2) ~ 0,
                                 TRUE ~ 1))
}

When I run this code, I can only see a new variable named variable_3 instead of creating c_2:c_10. Why is that? Would someone also explain why it doesn't work?


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>