如何在至少一个样本(每组)中打印值大于 0.1 的键数?

How to print the number of keys with the values greater than 0.1 in at least one sample (in each group)?

如何在至少一个样本(每组)中打印值大于 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

一个选项是 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

另一种选择:创建条件列,然后按汇总:

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