首页 > 解决方案 > R - 确认 2 列中的数据

问题描述

我正在尝试在 R 中编写以下逻辑。

if(there exists data in column A row A)
then(there must be data in column B row A)

有这个功能吗?

例子

预期输出:

Some sort of text here.
FALSE

标签: rvalidationlogic

解决方案


dat1
    A  B
1   3 NA
2  NA  2
3  NA NA
4  NA 10
5  NA  5
6   8 NA
7  NA  8
8   6  4
9  10  1
10  1 NA

g = which(is.na(dat1),T)
replace(dat1,g[!(duplicated(g[,1],fromLast = T)|duplicated(g[,1])),],"FALSE")
       A     B
1      3 FALSE
2  FALSE     2
3   <NA>  <NA>
4  FALSE    10
5  FALSE     5
6      8 FALSE
7  FALSE     8
8      6     4
9     10     1
10     1 FALSE

推荐阅读