【问题标题】:R - Vectorized Mean with WeightingR - 加权矢量化平均值
【发布时间】:2016-05-01 01:08:40
【问题描述】:

我目前能够使用以下代码快速计算我拥有的数百万个条目的数据集的平均值:

PosAvg = mean( curTweets$posScore[curTweets$posScore > 1])
uniqPosTweets = curTweets[ curTweets$posScore > abs(curTweets$negScore) ,]
UniqPosAvg = mean( uniqPosTweets$posScore )

但是,我想对这些进行加权,并且仍然以与上述相同的方式保持我这样做的效率。

curTweets$posScore / curTweets$negScore 可以取值 1、2、3、4、5。

假设我想给出以下权重:分别为 6、7、8、9、10。我使用这些数字来区分 posScore 的潜在值。实际权重在我的算法中计算。

有没有办法做到这一点?我不知道在保持这种效率的同时我会如何体重。我是否不得不遍历每个条目并单独计算贡献?

谢谢!

【问题讨论】:

  • c(1, 2, 3, 4, 5) * c(1, 2, 3, 4, 5) 将两个向量相乘得到元素乘积。
  • 我不确定我是否遵循如何在这里应用它。

标签: r vectorization weighted-average


【解决方案1】:
foo <- seq(5)
weights <- c(1, 1, 1, 1, 100)
vectorized_weighted_mean <- sum(foo * weights) / sum(weights)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    相关资源
    最近更新 更多