Home > Software design >  Pairing ifelse with dplyr in R dataframe
Pairing ifelse with dplyr in R dataframe

Time:06-26

In the dataframe df, I want to assign a condition such that for a given month and level, if value is 0 or 1 then assign "alone". If not assign "together". In the end, I want to retain the original dataset rows but only add the assign column which will have repeated values. Here is my attempt, the error and needed outcome:

df <- data.frame(level = rep(c("1","2","3"), each = 5),
month = rep(c("J","J","A"), each = 5),
val = c(0,0,1,1,0,2,1,1,1,1,4,6,1,0,1))

df <- df %>%
      dplyr::mutate(level, month, count)%>%
      dplyr::group_by(month, level) %>%
      dplyr::summarise(assign = ifelse(df$val %in% c(0,1), "alone", "together"))

Error:

Error in mutate_cols(): ! Problem with mutate() input ..3. ℹ ..3 = count. ✖ ..3 must be a vector, not a function.

Expected outcome

    level month val  assign

1      1     J   0  alone
2      1     J   0  alone
3      1     J   1  alone
4      1     J   1  alone
5      1     J   0  alone
6      2     J   2  together
7      2     J   1  together
8      2     J   1  together
9      2     J   1  together
10     2     J   1  together
11     3     A   4  together
12     3     A   6  together
13     3     A   1  together
14     3     A   0  together
15     3     A   1  together

CodePudding user response:

If I didn't get you wrong, maybe you can try with the following :

data.frame(level = rep(c("1","2","3"), each = 5),
           month = rep(c("J","J","A"), each = 5),
           val = c(0,0,1,1,0,2,1,1,1,1,4,6,1,0,1)) %>% 
        mutate(assign = ifelse(level == 0 | month == 0 | level == 1 | month == 1,
                      yes = "alone", no = "together"))

which will give you the following output :

   level month val   assign
1      1     J   0    alone
2      1     J   0    alone
3      1     J   1    alone
4      1     J   1    alone
5      1     J   0    alone
6      2     J   2 together
7      2     J   1 together
8      2     J   1 together
9      2     J   1 together
10     2     J   1 together
11     3     A   4 together
12     3     A   6 together
13     3     A   1 together
14     3     A   0 together
15     3     A   1 together

If that was not what you were specting, please let me know :)

CodePudding user response:

To return the original dataframe with the new column, you want to use mutate instead of summarise. Here, we can use all in the ifelse statement to limit to only those specific values in order to assign alone. You also don't need the first mutate statement in your code. Perhaps, you were wanting to use select from you original dataframe.

df %>%
  dplyr::group_by(month, level) %>%
  dplyr::mutate(assign = ifelse(all(val %in% c(0, 1)), "alone", "together"))

Output

   level month   val assign  
   <chr> <chr> <dbl> <chr>   
 1 1     J         0 alone   
 2 1     J         0 alone   
 3 1     J         1 alone   
 4 1     J         1 alone   
 5 1     J         0 alone   
 6 2     J         2 together
 7 2     J         1 together
 8 2     J         1 together
 9 2     J         1 together
10 2     J         1 together
11 3     A         4 together
12 3     A         6 together
13 3     A         1 together
14 3     A         0 together
15 3     A         1 together
  • Related