Mutate with dplyr using several conditions

I have a data frame (df) below, and I want to add an extra column resultusing dplyr, which will take the value 1, if z == "gone"and where xis the maximum value for the group y.

   y  x    z
1  a  3 gone
2  a  5 gone
3  a  8 gone
4  a  9 gone
5  a 10 gone
6  b  1     
7  b  2     
8  b  4     
9  b  6     
10 b  7     

If I just chose the maximum for each group, this would be:

df %>%
  group_by(y) %>%
  slice(which.max(x))

which will return:

   y  x  z
1  a 10  gone
2  b  7      

This is not what I want. I need to use the maximum value xfor each group in y, checking if there is z == "gone", and if TRUE1 otherwise, 0. It will look like this:

   y  x    z result
1  a  3 gone      0
2  a  5 gone      0
3  a  8 gone      0
4  a  9 gone      0
5  a 10 gone      1
6  b  1           0
7  b  2           0
8  b  4           0
9  b  6           0
10 b  7           0

I suppose I would use the conditional operator in mutate(), but I cannot find an example. Please inform.

+4
source share
2

dplyr :

df %>% group_by(y) %>% mutate(result = +(x == max(x) & z == 'gone'))

+(..) as.integer, 1 0. , . .

, , data.table dplyr R, "split-apply-comb":

#split data.frame by group
split.df <- split(df, df$y)

#apply required function to each group
lst <- lapply(split.df, function(dfx) {
        dfx$result <- +(dfx$x == max(dfx$x) & dfx$z == "gone")
        dfx})

#combine result in new data.frame
newdf <- do.call(rbind, lst)
+5

data.table. 'data.frame' 'data.table' (setDT(df)), 'y', 'x' 'gone' 'z', (as.integer) (:=) ( "" ).

library(data.table)
setDT(df)[, result := as.integer(x==max(x) & z=='gone') , by = y]
df
#    y  x    z result
# 1: a  3 gone      0
# 2: a  5 gone      0
# 3: a  8 gone      0
# 4: a  9 gone      0
# 5: a 10 gone      1
# 6: b  1           0
# 7: b  2           0
# 8: b  4           0
# 9: b  6           0
#10: b  7           0

ave base R

df$result <- with(df, +(ave(x, y, FUN=max)==x & z=='gone' ))
+5

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


All Articles