r - 验证特定列中是否至少有两列具有相同的值
问题描述
我有一个数据,我想看看我的变量是否在特定行中都具有唯一值假设我想分析 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 )
解决方案
在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))
推荐阅读
- c# - 处理大文件流(读+写字节)
- jquery - 检查所有不是将所有总和添加到总输入中
- c++ - 为什么我得到变量“节点”不是类型名称错误
- json - 使用 XSLT 转换将 XML 转换为 JSON 后有什么方法可以删除根节点
- node.js - 为什么在使用维度时,Google 分析中的总用户数会有所不同?
- java - 如何在springboot中为DomainArgumentResolver编写测试类?
- git - 撤消 Git 远程分支签出
- python - 如何从一行开始我的 excel 标题(第 10 行是我的数据开始的地方)
- amazon-web-services - 我可以在不使用 Route 53 的情况下将我的域和子域指向 AWS 托管吗?
- php - 正则表达式获取 MTOM 二进制 PDF 内容