【问题标题】:calling the results of each iteration in a series调用系列中每次迭代的结果
【发布时间】:2018-11-21 12:50:03
【问题描述】:

我运行了一个循环,结果保存在列表com 中。现在我必须调用每次迭代的结果(#iterations=2000)并计算值的平均值,如下所示:

l<-rbindlist(list(com[[1]], com[[2]], com[[3]],...com[[2000]]))[, .(values = mean(values)),
             by = variables][order(variables)]. 

我是 R 的初学者。这样做的简单方法是什么?

【问题讨论】:

  • 如果是列表,就做rbindlist(com)
  • 您的 com 条目的格式到底是什么?它们是数字向量吗,你想计算平均值吗?
  • 您需要data.table 解决方案吗?另外,请在com 中显示其中一个结果的示例。

标签: r data.table iteration series


【解决方案1】:

在您提供数据示例之前,这只是猜测。我假设您列表中的结果com 是数字向量。否则,此解决方案可能不起作用。

这是基础R,而不是data.table

示例数据:

set.seed(1)
com <- list(rnorm(100), rnorm(100), rnorm(100), rnorm(100), rnorm(100))

我们使用do.call将结果绑定在一起:

l <- do.call("rbind", com)

现在我们使用矢量化的rowMeans

rowMeans(l)

> rowMeans(l)
[1]  0.10888737 -0.03780808  0.02967354  0.05160186 -0.03913424

【讨论】:

    猜你喜欢
    • 2021-10-05
    • 2018-06-11
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2021-10-02
    • 2015-03-19
    • 2013-12-18
    相关资源
    最近更新 更多