根据 dplyr 中的列保留组之间的不同行
Keep the distinct rows among groups based on a column in dplyr
我的数据框是这样的
df <- data.frame(gene=c("A","B","C","A","B","D"),
origin=rep(c("old","new"),each=3),
value=sample(rnorm(10,2),6))
gene origin value
1 A old 1.5566908
2 B old 1.3000358
3 C old 0.7668213
4 A new 2.5274712
5 B new 2.2434525
6 D new 2.0758326
我想在原始列中保留新旧组之间的不同基因。
我希望我的数据看起来像这样
gene origin value
3 C old 0.7668213
6 D new 2.0758326
按'gene'分组,在filter
中查找不同'origin'的数量为1
library(dplyr)
df %>%
group_by(gene) %>%
filter(n_distinct(origin) == 1) %>%
ungroup
我的数据框是这样的
df <- data.frame(gene=c("A","B","C","A","B","D"),
origin=rep(c("old","new"),each=3),
value=sample(rnorm(10,2),6))
gene origin value
1 A old 1.5566908
2 B old 1.3000358
3 C old 0.7668213
4 A new 2.5274712
5 B new 2.2434525
6 D new 2.0758326
我想在原始列中保留新旧组之间的不同基因。 我希望我的数据看起来像这样
gene origin value
3 C old 0.7668213
6 D new 2.0758326
按'gene'分组,在filter
library(dplyr)
df %>%
group_by(gene) %>%
filter(n_distinct(origin) == 1) %>%
ungroup