R 如何根据阈值计算每列中的行数

R 如何根据阈值计算每列中的行数,r,R,我有以下数据集: dat <- structure(list(Probes = structure(1:6, .Label = c("1415670_at", "1415671_at", "1415672_at", "1415673_at", "1415674_a_at", "1415675_at" ), class = "factor"), Genes = structure(c(2L, 1L, 4L, 5L, 6L, 3L), .Label = c("Atp6v0d1", "Copg1

我有以下数据集:

dat <- structure(list(Probes = structure(1:6, .Label = c("1415670_at",
"1415671_at", "1415672_at", "1415673_at", "1415674_a_at", "1415675_at"
), class = "factor"), Genes = structure(c(2L, 1L, 4L, 5L, 6L,
3L), .Label = c("Atp6v0d1", "Copg1", "Dpm2", "Golga7", "Psph",
"Trappc4"), class = "factor"), bCD.ID.LN = c(1.133, 1.068, 1.01,
0.943, 1.048, 1.053), bCD.ID.LV = c(1.049, 1.006, 0.883, 0.799,
0.96, 1.104), bCD.ID.SP = c(1.124, 1.234, 1.029, 1.064, 1.118,
1.057), bCD.IP.LV = c(1.013, 1.082, 1.061, 0.982, 1.191, 1.053
), bCD.IP.SP = c(0.986, 1.102, 1.085, 0.997, 1.141, 1.041)), .Names = c("Probes",
"Genes", "bCD.ID.LN", "bCD.ID.LV", "bCD.ID.SP", "bCD.IP.LV",
"bCD.IP.SP"), row.names = c(NA, 6L), class = "data.frame")
值大于1.1的第三列向前计数行的tod 所以最后看起来是这样的:

> dat

        Probes    Genes bCD.ID.LN bCD.ID.LV bCD.ID.SP bCD.IP.LV bCD.IP.SP
1   1415670_at    Copg1     1.133     1.049     1.124     1.013     0.986
2   1415671_at Atp6v0d1     1.068     1.006     1.234     1.082     1.102
3   1415672_at   Golga7     1.010     0.883     1.029     1.061     1.085
4   1415673_at     Psph     0.943     0.799     1.064     0.982     0.997
5 1415674_a_at  Trappc4     1.048     0.960     1.118     1.191     1.141
6   1415675_at     Dpm2     1.053     1.104     1.057     1.053     1.041
bCD.ID.LN 1
bCD.ID.LV 1
bCD.ID.SP 3
bCD.IP.LV 1
bCD.IP.SP 2

我该怎么做呢?

我们可以根据数据集中的数字列在逻辑矩阵上尝试
colSums

Count <- colSums(dat[-(1:2)] > 1.1, na.rm=TRUE)

如果它是一个大数据集,那么转换为逻辑矩阵可能不会节省内存,在这种情况下,最好使用
vapply

vapply(dat[-(1:2)], function(x) sum(x > 1.1, na.rm=TRUE), 0)

这里有一个使用lappy()的替代版本

或者,如果您希望将其作为data.frame()使用,则


另一个版本,这次使用dplyr

dat %>% 
select(-c(Probes, Genes)) %>% 
summarise_each (funs(sum((. > 1.1))))

谢谢太好了。顺便说一句,在我的实际案例中,我怎么会得到所有的NA?这是我的代码:@neversaint如果原始数据集中有NA值,那么在转换为逻辑矩阵时,这些值仍然存在。因此,使用
colSums(…,na.rm=TRUE)
。更新了帖子。为什么要发布两个不同的答案?最好将它们合并为一个。我认为最好将base的lappy()与dplyr分开。希望没问题。通常情况下,给出两个单独的答案会给人留下这样的印象:你太渴望获得声誉积分了。尤其是当你的答案这么短的时候。在一个答案中给出几个备选方案是完全正确的。
lapply(dat[-c(1:2)], function(x) length(which(x > 1.1)))
data.frame( lapply(dat[-c(1:2)], function(x) length(which(x > 1.1))))
dat %>% 
select(-c(Probes, Genes)) %>% 
summarise_each (funs(sum((. > 1.1))))