dplyr用于计算条件中变量的普遍性 [英] dplyr to calculate of prevalence of a variable in a condition

查看:81
本文介绍了dplyr用于计算条件中变量的普遍性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是dplyr领域的新手,非常抱歉,如果这个问题听起来很简单,基本上是
,我有兴趣计算每列中大于0.5的条目数。如果它们小于0.5,我认为它们为零。我不介意有一个向量来存储这个数字。

I am new in the dplyr world - so sorry if the question might sound simple, basically, I am interested in calculating the number of entries that are larger than 0.5 for each column. If they are lower than 0.5 I consider them as zero. I don't mind having a vector, that stores this number.

这里是示例

messy <- data.frame(samples = c("s1", "s2", "s3", "s4"),
                    o1 = c(0.5, 0.7, 0.8, 0.6),
                    o2 = c(0.2, 0.8, 0.8, 0.1),
                    o3 = c(0.9, 0.2, 0.0, 0.1),
                    o4 = c(0.1, 0.6, 0.4, 0.4))
bb <- gather(messy, otu, counts, o1:o4)

bb %>% filter(counts > 0.5) %>% group_by(otu) %>% summarize(fre=n())
bb$fre/4

**更新,
我相信示例中的代码就是我想要的。

** update, I believe the code in the example is what I wanted to have.

推荐答案

您可以执行 colSums(messy> 0.5)
这不使用 dplyr ,但是它非常简单且有效。

You can do colSums(messy > 0.5). This doesn't use dplyr but it is very simple and efficient.

这篇关于dplyr用于计算条件中变量的普遍性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆