本文介绍了为什么all.equal在dplyr的mutate函数中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下代码:
library(dplyr)
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)
myf <- function(patientID, age, diabetes, status) { isTRUE(all.equal(age, 34))}
mutate(patientdata, isAge34 = myf(patientID, age, diabetes, status))
我写了 myf
返回 TRUE
表示 age == 34
的行,但这不起作用:
I wrote myf
to return TRUE
for the row where age == 34
, but this doesn't work:
patientID age diabetes status isAge34
1 1 25 Type1 Poor FALSE
2 2 34 Type2 Improved FALSE
3 3 28 Type1 Excellent FALSE
4 4 52 Type1 Poor FALSE
为什么没有用?我做错了吗?
Why didn't this work? Did I doing something wrong?
编辑:这是一个人为的简化示例。实际上,我的函数内部逻辑复杂得多。
This is a contrived, simplified example. In reality, I have much more complicated logic inside of my function.
我发问的动机:
- 我认为我应该更喜欢
isTRUE(all.equal())
而不是==
因为这是R的处理方式。
- I thought that I was supposed to prefer
isTRUE(all.equal())
over==
because that's the R way of doing things.
,:
推荐答案
为, all.equal()$ c $
As @DavidArenburg said, all.equal()
is not vectorized.
以下代码将起作用:
mutate(patientdata, isAge34 = age == 34)
这篇关于为什么all.equal在dplyr的mutate函数中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!