首页 > 解决方案 > 验证特定列中是否至少有两列具有相同的值

问题描述

我有一个数据,我想看看我的变量是否在特定行中都具有唯一值假设我想分析 D 行

我的数据

Name      F     S     T
A         1     2     3
B         2     3     4
C         3     4     5
D         4     5     6


> TRUE (because all the three variables have unique value)

第二个例子

Name      F     S     T
A         1     2     3
B         2     3     4
C         3     4     5
D         4     5     4

>False (because F and T have the same value in row D )

标签: rdataframestatistics

解决方案


base R

f1 <- function(dat, ind) {
    
    tmp <- unlist(dat[ind, -1])
    length(unique(tmp)) == length(tmp)
}

-测试

> f1(df, 4)
[1] TRUE
> f1(df1, 4)
[1] FALSE

数据

df <- structure(list(Name = c("A", "B", "C", "D"), F = 1:4, S = 2:5, 
    T = 3:6), class = "data.frame", row.names = c(NA, -4L))
df1 <- structure(list(Name = c("A", "B", "C", "D"), F = 1:4, S = 2:5, 
    T = c(3L, 4L, 5L, 4L)), class = "data.frame", row.names = c(NA, 
-4L))

推荐阅读