r - 如何选择非唯一的列组合?
问题描述
我的数据如下所示:
counts <- data.frame(
pos = c(101, 101, 101, 102, 102, 102, 103, 103, 103, 101, 101, 101),
chr = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4),
subj = c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C")
)
pos
应该只属于一个 unique chr
,但这里 pos 101 属于 chr 1 和 4。
我可以像这样检测这种情况:
counts %>% select(pos, chr) %>%
group_by(pos) %>%
summarise(n_chrs = length(unique(chr))) %>%
filter(n_chrs > 1)
这将返回pos
具有多于 to 的chr
值:
A tibble: 1 x 2
pos n_chrs
<dbl> <int>
1 101 2
我想知道chr
涉及哪些值,例如:
pos chr
1 101 1
2 101 4
谢谢!
解决方案
你可以这样做:
library(dplyr)
counts %>%
group_by(pos) %>%
distinct(chr) %>%
filter(n() > 1)
输出:
# A tibble: 2 x 2
# Groups: pos [1]
pos chr
<dbl> <dbl>
1 101 1
2 101 4
推荐阅读
- css - 带有悬停时出现的文本的容器
- json - 如何在 Scala 中将 Json 转换为 XML?
- python - 将数字拆分为其中的素数
- javascript - 快速响应操作以将字符串发送到客户端
- python - 查找多个相同数字的立即数的优化或最佳方法
- excel - 如何自动过滤许多列?
- nginx - nginx 速率限制,某些路径除外
- python - Pandas:逐个元素合并两个列表列
- microsoft-graph-api - Microsoft 图形 API:/me/drive/root/search - 没有找到文件的路径
- html - 段落的子元素应用 CSS 后,标签未下推至 html 元素下方