在 R 中使用 apply() 计算每列范围内的单元格数

Using apply() in R to count the number of cells within a range for each column

我有一个大数据框,其中包含 0 到 1 之间的数字,我试图计算每列中 0.4 到 0.6 之间的值的数量。我已经成功计算了均值、中位数等。使用 apply 输出包含列均值的向量:

column_mean <- apply(x, 2, mean, na.rm=TRUE)

但我不太明白如何修改 apply() 来计算某个范围内的值的数量。我在想这样的事情,但在所有列中:

column_counts <- apply(x, 2, sum(x$c1 > 0.4 & x$c1 < 0.6), na.rm=TRUE)

感谢您的帮助!

是否回答:

> df <- data.frame(Col1 = rnorm(10),
+                  Col2 = rnorm(10),
+                  Col3 = rnorm(10))
> df
          Col1       Col2       Col3
1   0.73804784  1.7342752 -1.0906748
2   1.65272822 -1.2936601  0.4721306
3   0.41988220  0.1148715 -0.3010973
4   0.19199975  1.2164140  0.7646785
5   0.09016752 -1.7179874 -0.5046282
6  -1.59440039  1.2948078 -0.3152287
7  -0.74238335 -0.6169977  0.8392895
8   0.28572911  0.8212279  0.5394922
9  -1.71357200  2.0856380  0.3221748
10 -0.29211236  0.5290523  0.4206429
> sapply(df, function(x) sum(x > 0.4 & x < 0.6))
Col1 Col2 Col3 
   1    1    3 
>

创建自定义函数:

count_interval <- function(x) { 
  return(length(x[!is.na(x) & x>0.4 & x<0.6])) 
}

然后像以前一样使用它:

column_counts <- apply(x, 2, count_interval)

首先提示一下,您应该提供一个示例供我们使用,检查 https://whosebug.com/help/minimal-reproducible-example

您可以创建一个函数来进行特定测试,然后将其传递给应用:

test = function(a){sum(a>=0.4 & a<=0.6)}
column_counts = apply(x, 2, test)