在基于多列的条件下使用dplyr mutate [英] Using dplyr mutate with conditions based on multiple columns

查看:115
本文介绍了在基于多列的条件下使用dplyr mutate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有NA的情况下,以下代码将按预期工作:如果第一行中包含2,则新变量的值为2;否则为0.如果不是,我要检查是否有任何值是1;如果不是,则检查是否为0.如果不是,那么所有都必须是NA.

Without NAs, the following code would work as intended: if the first row has any 2's in it, the new variable takes a value of 2; if not, I want to check if any of the values are 1; if not, check if any are 0; if not, then all must be NA.

一旦我将NA添加到数据框中,它将不再起作用,而且我似乎无法弄清原因:

Once I add NAs into the data frame, it no longer works and I can't seem to figure out why:

V1 <- c(NA,1,2,0,0)
V2 <- c(0,0,2,1,1)
V3 <- c(NA,0,2,1,0)

V <- cbind(V1,V2,V3)

V <- mutate(V,V4 = ifelse(V1 == 2|V2==2|V3==2, 2, 
ifelse(V1==1|V2==1|V3==1, 1, ifelse(V1==0|V2==0|V3==0,0,NA))))

预期输出:

  V1 V2 V3 V4
1 NA  0 NA  0
2  1  0  0  1
3  2  2  2  2
4  0  1  1  1
5  0  1  0  1

实际输出:

  V1 V2 V3 V4
1 NA  0 NA NA
2  1  0  0  1
3  2  2  2  2
4  0  1  1  1
5  0  1  0  1

推荐答案

如果您这样做,它将按预期工作:

It works as intended if you do:

mutate(V, V4 = case_when(
  V1 == 2 | V2 == 2 | V3 == 2 ~ 2,
  V1 == 1 | V2 == 1 | V3 == 1 ~ 1,
  V1 == 0 | V2 == 0 | V3 == 0 ~ 0
))

此外,您应该使用data.frame()data_frame()tibble()之一而不是cbind(),以使V对象更符合dplyr函数,后者期望使用数据帧而不是矩阵(即由cbind()产生.

Also, you should use one of data.frame(), data_frame() or tibble() instead of cbind() to make the V object more compliant to dplyr functions, which expect a data frame instead of a matrix (which is what gets produced by cbind().

这篇关于在基于多列的条件下使用dplyr mutate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆