如何在至少一个样本(每组中)中打印大于0.1的键数。 我正在使用这些命令来分隔每个组并计算满足该组的键数 以上条件。如何使用tidyverse中的group_by(Group)函数立即执行此操作?
input <- tribble(
~Key, ~Group, ~sample1, ~sample2, ~sample3, ~samplen,
"a1", "ABC", 0, 0.1, 0.2, 0,
"a2", "ABC", 1, 2, 3, 0,
"a3", "DEF", 0, 0, 0, 0,
"a4", "DEF", 2, 22, 23, 2,
"a5", "DEF", 0, 0, 0.1, 0
)
ABC <- input %>%
filter(Group=="ABC")
dat<-ABC[,-c(1:1)]
aux<-apply(dat,2,function(x){x>=0.1})
sel.gene<-apply(aux,2,sum)
sel.gene.1<-which(sel.gene>=1)
ABC_output <-dat[,sel.gene.1]
dim(ABC_output)
DEF <- input %>%
filter(Group=="DEF")
dat<-DEF[,-c(1:1)]
aux<-apply(dat,2,function(x){x>=0.1})
sel.gene<-apply(aux,2,sum)
sel.gene.1<-which(sel.gene>=1)
DEF_output <-dat[,sel.gene.1]
dim(DEF_output)
Desired_output
ABC 2
DEF 1
答案 0 :(得分:1)
一个选项是filter
数据框首先使用filter_at
(示例列)+ any_vars
,然后count
组:
input %>%
filter_at(vars(starts_with('sample')), any_vars(. > 0.1)) %>%
count(Group)
# A tibble: 2 x 2
# Group n
# <chr> <int>
#1 ABC 2
#2 DEF 1
另一个选项:创建一个条件列,然后按 Group 汇总:
input %>%
mutate(n = rowSums(select(., starts_with('sample')) > 0.1) > 0) %>%
group_by(Group) %>%
summarise(n = sum(n))
# A tibble: 2 x 2
# Group n
# <chr> <int>
#1 ABC 2
#2 DEF 1