简体   繁体   English

R中的条件数据帧突变与magrittr和dplyr

[英]Conditional dataframe mutations in R with magrittr and dplyr

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. 我想使用magrittr和dplyr的简洁性来根据其他列中的值在列的子集中的行之间复制单个值。 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) : 取数据帧df <- data.frame(a = 1:5, b = 6:10, x = 11:15, y = 16:20) : 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 = 5的行,我想将xy的值替换为b = 7的行中的值,以给出:

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. 但这是不正确的,因为它将两个值替换为x的值,而不是xy

Thanks for the advice. 感谢您的建议。

You could do it using mutate_each and replace : 您可以使用mutate_eachreplace

df %>% mutate_each(funs(replace(., a==5, nth(., which(b==7)))), x, y)

Output: 输出:

  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

Or as per @docendodiscimus 's comment it can be shortened further to (and probably [ is also better than which ): 或者根据@docendodiscimus的评论,它可以进一步缩短(并且可能[也比which更好]:

df %>% mutate_each(funs(replace(., a==5, .[b==7])), x, y)

Just to mention the data.table solution would be: data.table解决方案将是:

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]]

If your main requirement is to apply the function within a longer dplyr-pipe, you could do something like the following example: 如果您的主要要求是在较长的dplyr-pipe中应用该函数,则可以执行类似以下示例的操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM