我正在尝试运行以下代码:
x = 0
y = 0
newdata <- subset(data, subject_ids == 25773861)
for(i in newdata$classification_id){
if(newdata$value == "Yes"){
x = x + 1
} else {
y = y + 1
}
}
但是请继续收到此警告:
Warning messages:
1: In if (newdata$value_simple == 0) { :
the condition has length > 1 and only the first element will be used
2: In if (newdata$value_simple == 0) { :
the condition has length > 1 and only the first element will be used
在解决此问题方面有任何建议或帮助吗?
答案 0 :(得分:0)
的通用代码
if(condition){
some code
}else{
some code}
仅查看向量中的第一个值。因此,基本上是警告您,它仅查看对象newdata $ value中的第一个值。我假设您正在x或y中获得全部,而不是您想要的拆分。
我将在该代码中修复的两件事从for循环的前两行开始
x = 0
y = 0
newdata <- subset(data, subject_ids == 25773861)
for(i in seq_along(newdata$classification_id)){ #seq_along makes a vector 1 to the length of your vector
if(newdata$value[[i]] == "Yes"){ #This will subset the newdata$Value into single values
x = x + 1
} else {
y = y + 1
}
}
另一种选择是使用tidyverse,前提是您已安装tidyverse
library(tidyverse)
data %>%
filter(subject_ids == 25773861) %>%
group_by(value) %>%
count()