R-计算每列中某些值的数量 [英] R - Count numbers of certain values in each column
本文介绍了R-计算每列中某些值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现了类似的问题,但是没有一个问题说明如何对数据框的每一列进行操作。
I have found similar questions to mine, but none of them explains how to do that for each column of a dataframe.
我有一个像这样的数据框:
I have a dataframe like this:
x1 = seq(12, 200, length=20)
x2 = seq(50, 120, length=20)
x3 = seq(40, 250, length=20)
x4 = seq(100,130, length=20)
x5 = seq(10, 300, length=20)
df = data.frame(V1=x1, V2=x2, V3=x3, V4=x4, V5=x5)
现在我想获取每列大于120的值的数量。
Now I want to get the number of values that are greater than 120 for each column.
我尝试过:
nrow(df[,1] >120)
那没用,它说为0,但它不是真的,我也想自动完成所有列。
That didnt work, it says 0, but its not true, and also I want to do all columns automatically.
推荐答案
您可以使用 tidyverse
解决
library(tidyverse)
df%>%
gather(x, value, V1:V5)%>%
group_by(x)%>%
tally(value > 120)
# A tibble: 5 x 2
x n
<chr> <int>
1 V1 9
2 V2 0
3 V3 12
4 V4 7
5 V5 12
希望这会有所帮助。
这篇关于R-计算每列中某些值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文