将mutate与条件值组合使用

rda*_*tor 68 r dplyr mutate

在具有四列的大型数据框("myfile")中,我必须添加第五列,其中值基于前四列.最近我成为了dplyr的忠实粉丝,主要是因为它在大型数据集中的速度.所以我想知道我是否可以使用mutate函数处理我的问题.

我的数据帧(实际上是它的较短版本)看起来有点像这样:

  V1 V2 V3 V4
1  1  2  3  5
2  2  4  4  1
3  1  4  1  1
4  4  5  1  3
5  5  5  5  4
...
Run Code Online (Sandbox Code Playgroud)

第五列(V5)的值基于一些条件规则:

if (V1==1 & V2!=4) {
  V5 <- 1
} else if (V2==4 & V3!=1) {
  V5 <- 2
} else {
  V5 <- 0
}
Run Code Online (Sandbox Code Playgroud)

现在我想使用mutate函数在所有行上使用这些规则(所以我不必使用慢循环).像这样的东西(是的,我知道它不会这样工作!):

myfile <- mutate(myfile, if (V1==1 & V2!=4){V5 = 1}
    else if (V2==4 & V3!=1){V5 = 2}
    else {V5 = 0})
Run Code Online (Sandbox Code Playgroud)

这应该是结果:

  V1 V2 V3 V4 V5
1  1  2  3  5  1
2  2  4  4  1  2
3  1  4  1  1  0
4  4  5  1  3  0
5  5  5  5  4  0
Run Code Online (Sandbox Code Playgroud)

怎么做dplyr

G. *_*eck 83

试试这个:

myfile %>% mutate(V5 = (V1 == 1 & V2 != 4) + 2 * (V2 == 4 & V3 != 1))
Run Code Online (Sandbox Code Playgroud)

赠送:

  V1 V2 V3 V4 V5
1  1  2  3  5  1
2  2  4  4  1  2
3  1  4  1  1  0
4  4  5  1  3  0
5  5  5  5  4  0
Run Code Online (Sandbox Code Playgroud)

或这个:

myfile %>% mutate(V5 = ifelse(V1 == 1 & V2 != 4, 1, ifelse(V2 == 4 & V3 != 1, 2, 0)))
Run Code Online (Sandbox Code Playgroud)

赠送:

  V1 V2 V3 V4 V5
1  1  2  3  5  1
2  2  4  4  1  2
3  1  4  1  1  0
4  4  5  1  3  0
5  5  5  5  4  0
Run Code Online (Sandbox Code Playgroud)

建议您为数据框架获得更好的名称.myfile使它看起来好像拥有一个文件名.

上面使用了这个输入:

myfile <- 
structure(list(V1 = c(1L, 2L, 1L, 4L, 5L), V2 = c(2L, 4L, 4L, 
5L, 5L), V3 = c(3L, 4L, 1L, 1L, 5L), V4 = c(5L, 1L, 1L, 3L, 4L
)), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5"))
Run Code Online (Sandbox Code Playgroud)

更新1 由于最初发布的dplyr已更改%.%%>%已相应修改了答案.

现在更新2 dplyr case_when提供了另一种解决方案:

myfile %>% 
       mutate(V5 = case_when(V1 == 1 & V2 != 4 ~ 1, 
                             V2 == 4 & V3 != 1 ~ 2,
                             TRUE ~ 0))
Run Code Online (Sandbox Code Playgroud)

  • 我发现了一种允许你不嵌套`ifelse`语句的方法:`myfile%>%mutate(V5 = ifelse(V1 == 1&V2!= 4,1,0),V5 = ifelse(V2 == 4) &V3!= 1,2,V5))` (5认同)

Dan*_*iel 19

有了dplyr 0.7.2,你可以使用非常有用的case_when功能:

x=read.table(
 text="V1 V2 V3 V4
 1  1  2  3  5
 2  2  4  4  1
 3  1  4  1  1
 4  4  5  1  3
 5  5  5  5  4")
x$V5 = case_when(x$V1==1 & x$V2!=4 ~ 1,
                 x$V2==4 & x$V3!=1 ~ 2,
                 TRUE ~ 0)
Run Code Online (Sandbox Code Playgroud)

用表示dplyr::mutate,它给出:

x = x %>% mutate(
     V5 = case_when(
         V1==1 & V2!=4 ~ 1,
         V2==4 & V3!=1 ~ 2,
         TRUE ~ 0
     )
)
Run Code Online (Sandbox Code Playgroud)

请注意,NA不予特别处理,因为它可能会产生误导.NA仅当没有条件匹配时,该函数才会返回.如果你输入一行TRUE ~ ...,就像我在我的例子中所做的那样,返回值将永远不会NA.

因此,你必须告诉表现力case_whenNA它所属通过增添一份声明is.na(x$V1) | is.na(x$V3) ~ NA_integer_.提示:这个dplyr::coalesce()功能有时候非常有用!

此外,请注意,NA单独通常不起作用,你必须提出特殊NA值:NA_integer_,NA_character_NA_real_.


Jak*_*her 11

它看起来像derivedFactormosaic包装是专为这一点.在这个例子中,它看起来像:

library(mosaic)
myfile <- mutate(myfile, V5 = derivedFactor(
    "1" = (V1==1 & V2!=4),
    "2" = (V2==4 & V3!=1),
    .method = "first",
    .default = 0
    ))
Run Code Online (Sandbox Code Playgroud)

(如果您希望结果是数字而不是因子,请derivedFactoras.numeric.包装.)

请注意,.default结合选项.method = "first"设置"else"条件 - 此方法在帮助文件中描述derivedFactor.

  • @Fato39是的,`mosaic :: derivedFactor`系列函数非常慢.如果您弄清楚原因,请回答我的问题:/sf/ask/2365138401/.我很高兴从你的其他评论中看到`dplyr :: case_when`更快 - 我将不得不切换到那个. (3认同)