with - r mutate




Conditional dataframe mutations in R with magrittr and dplyr (2)

If your main requirement is to apply the function within a longer dplyr-pipe, you could do something like the following example:

foo <- function(df, cols = c("x", "y")) {
  df[df$a == 5, cols] <- df[df$b == 7, cols]
  df
}

df %>% ... %>% foo(c("x", "y")) %>% ... 
#  a  b  x  y
#1 1  6 11 16
#2 2  7 12 17
#3 3  8 13 18
#4 4  9 14 19
#5 5 10 12 17

I would like to use the succinctness of magrittr and dplyr to copy single values between rows in a subset of columns based on the values in other columns. This is a simple example; I want to apply this idea to many columns of a large dataset with multiple conditions within a long pipe of commands.

Take the dataframe df <- data.frame(a = 1:5, b = 6:10, x = 11:15, y = 16:20):

a   b   x   y

1   6   11  16
2   7   12  17
3   8   13  18
4   9   14  19
5   10  15  20

For the row where a = 5, I would like to replace the values of x and y with those in the row where b = 7, to give:

a   b   x   y

1   6   11  16
2   7   12  17
3   8   13  18
4   9   14  19
5   10  12  17

This attempt fails:

foo <- function(x){ifelse(df$a == 5, df[df$b == 7, .(df$x)], x)}
df %<>%  mutate_each(funs(foo), x, y)

The closest I can get is:

bar <- function(x){ifelse(df$a == 5, df[df$b == 7, "x"], x)}
df %<>%  mutate_each(funs(bar), x, y)

but this is incorrect as it replaces both values with the value from x, rather than x and y respectively.

Thanks for the advice.


Just to mention the data.table solution would be:

require(data.table)
setDT(df)[a == 5, c("x", "y") := df[b == 7, .SD, .SDcols = c("x", "y")]]

> df
   a  b  x  y
1: 1  6 11 16
2: 2  7 12 17
3: 3  8 13 18
4: 4  9 14 19
5: 5 10 12 17

Alternatively, you could also use:

cols <- c("x", "y")
setDT(df)[a == 5, (cols) := df[b == 7, .SD, .SDcols = cols]]
# or 
cols <- c("x", "y")
setDT(df)[a == 5, (cols) := df[b == 7, cols, with = FALSE]]




magrittr