【发布时间】:2017-06-07 07:59:18
【问题描述】:
这是我的数据框
Key Q1 Q2
1 1 NA
2 2 1
3 8 NA
4 3 2
5 1 32
6 5 3
我想验证数据是否符合规则
规则1:
if Q1==1, Q2 can only have 1 & 2
规则2:
if Q1!=1, Q2 has to be empty/without any value
我希望得到如下结果:
Key Q1 Q2 Result
1 1 NA TRUE
2 2 1 TRUE
3 8 NA FALSE
4 3 2 TRUE
5 1 32 FALSE
6 5 3 FALSE
但是,我收到警告错误“条件的长度 > 1,并且只会使用第一个元素”。
【问题讨论】:
-
最好为 df 添加代码而不是屏幕截图。真的很有帮助
-
@agerom 当然。已编辑
-
还有什么意思是空的/没有任何价值? (在数字列中“空”不存在。插入了 NA)
-
你是怎么得到
I got the warning error "the condition has length > 1 and only the first element will be used".的。请添加您尝试过的代码 -
第一行Q1为1,Q2为NA或空白。那么,为什么它是 TRUE 呢?你的规则是
Q1=1, Q2 can only have 1 & 2
标签: r validation logical-operators na