r - 如何计算未排序数据集的中位数

标签 r median

我有数千个这样的数据集:

>student1
    quantities score
[1]          4    10         
[2]          1    12         
[3]         78     5         
[4]          6   294

我想计算该学生的分数中位数。对于每个分数,我们都有一些数量。在本例中,我希望它返回 5,因为中位数是 78 个 5 之一。

我在这里看过一些帖子,例如 how to calculate the median on grouped dataset? ,但我无法使用它,因为我有数千个数据集。

我还尝试安装aroma.light包和matrixstats包,但我仍然无法使用“weighted.median function”的东西。它告诉我

Error: could not find function "weightedMedians"

好的,上面只是一个例子,我的真实数据集是这样的:

>test
     [,1]          [,2]
info    3            10
info    2            20
        4      86779637
        1        135777
        7          2342

但是当我尝试使用

>rep(test[, 1], test[, 2])

出现了

Error in rep(test[, 1], test[, 2]) : invalid 'times' argument
In addition: Warning message:
NAs introduced by coercion 

我现在能做什么?

最佳答案

您可以使用:

median(rep(student1$score, student1$quantities))

这相对较快(对于 100k 行的模拟数据集只需要几秒钟)

关于r - 如何计算未排序数据集的中位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24649813/

相关文章:

arrays - 找到两个排序数组的中位数的时间复杂度

python - Pandas 索引中值而不是值中值

r - 在长数据帧的特定时间内对字符串变量进行计数然后求和

r - ggplot2:快速热图绘制、 reshape ?

R: strptime() 和 is.na() 意外的结果

r - 获取使用purrr::map创建的列表项的名称

r - 将函数列表应用于 R 中 data.table 的每一列

netcdf - 使用 nco 计算 nc 文件的中位数

javascript - 在稀疏 Javascript 数组上执行查找

mysql - 我的 mySQL 调用只返回一个平均值,而它应该返回所有平均值