我正在使用case_when
功能。这是我的可复制数据:
mydata=data.frame(x=c(5,0,0),y=c(0,0,5),z=c(0,0,0))
我想创建一个新列,例如newcolumn
,它导致case_when函数中的AND运算,例如:
mydata=mydata%>%mutate(newcolumn=case_when(
(x>y)&(y=z)~"something",
(x=y)&(y=z)~"something else",
TRUE~"other"))
输出结果是:newcolumn=c("other","other","other")
但是,所需的输出应该是:
newcolumn=c("something","something else","other")
我在这里错过了一个非常明显的问题吗?也许我正在寻找但没有看到这里发生了什么。
您的帮助将不胜感激。
使用==
代替=
:
mydata %>%
mutate(newcolumn = case_when(
(x>y) & (y==z) ~ "something",
(x==y) & (y==z) ~ "something else",
TRUE ~ "other"))
应给您:
x y z newcolumn
1 5 0 0 something
2 0 0 0 something else
3 0 5 0 other