【问题标题】:Aggregate result in a list在列表中汇总结果
【发布时间】:2019-02-22 18:03:36
【问题描述】:

我想知道如何创建所有状态的列及其对应的时间(每个列表对应一个 id)。Qmatrix 并不重要,因为它保持不变。

 ``$ :List of 3
  ..$ states : num [1:3] 1 2 2
  ..$ times  : num [1:3] 0 5.23 15
  ..$ qmatrix: num [1:3, 1:3] -0.11 0.05 0.02 0.1 -0.15 0.07 0.01 0.1 -0.09
 $ :List of 3
  ..$ states : num [1:6] 1 2 3 2 1 1
  ..$ times  : num [1:6] 0 0.91 9.23 9.24 9.65 ...
  ..$ qmatrix: num [1:3, 1:3] -0.11 0.05 0.02 0.1 -0.15 0.07 0.01 0.1 -0.09
 $ :List of 3
  ..$ states : num [1:2] 1 1
  ..$ times  : num [1:2] 0 15
  ..$ qmatrix: num [1:3, 1:3] -0.11 0.05 0.02 0.1 -0.15 0.07 0.01 0.1 -0.09
 $ :List of 3
  ..$ states : num [1:4] 1 2 3 3
  ..$ times  : num [1:4] 0 10.7 13.7 15
  ..$ qmatrix: num [1:3, 1:3] -0.11 0.05 0.02 0.1 -0.15 0.07 0.01 0.1 -0.09
 $ :List of 3
  ..$ states : num [1:4] 1 2 3 3
  ..$ times  : num [1:4] 0 7.32 8.87 15
  ..$ qmatrix: num [1:3, 1:3] -0.11 0.05 0.02 0.1 -0.15 0.07 0.01 0.1 -0.09
 $ :List of 3
  ..$ states : num [1:3] 1 2 2
  ..$ times  : num [1:3] 0 7.07 15
  ..$ qmatrix: num [1:3, 1:3] -0.11 0.05 0.02 0.1 -0.15 0.07 0.01 0.1 -0.09
 $ :List of 3
  ..$ states : num [1:3] 1 2 2
  ..$ times  : num [1:3] 0 0.901 15
  ..$ qmatrix: num [1:3, 1:3] -0.11 0.05 0.02 0.1 -0.15 0.07 0.01 0.1 -0.09
 $ :List of 3
  ..$ states : num [1:4] 1 3 2 2
  ..$ times  : num [1:4] 0 5.85 6.26 15
  ..$ qmatrix: num [1:3, 1:3] -0.11 0.05 0.02 0.1 -0.15 0.07 0.01 0.1 -0.09
 $ :List of 3
  ..$ states : num [1:4] 1 2 3 3
  ..$ times  : num [1:4] 0 11.5 13 15
  ..$ qmatrix: num [1:3, 1:3] -0.11 0.05 0.02 0.1 -0.15 0.07 0.01 0.1 -0.09

我希望它是这种形式:

id  state   Time
1   1   0
1   3   15
2   1   0
2   3   9.666
2   2   0
2   3   10.5

enter image description here

【问题讨论】:

  • 请提供一个示例输出 data.frame 以便我们知道最终输出应该是什么样子
  • 谢谢@AndrewTaylor。我已经编辑了我的问题并展示了我需要数据的方式。如果你能帮助我,感激不尽
  • 很抱歉,但从列表和我们的示例输出中,我没有看到逻辑。你能解释一下你是如何从列表中得到输出的吗?此外,我们需要输入数据(您的列表)才能成为可重现示例的一部分。请创建一个测试数据集,或 dput() 您的列表并将其添加到帖子中。

标签: r list dataframe markov-models


【解决方案1】:

以下内容应返回包含 id、状态和时间列的数据框。 id 将基于列表项名称。如果使用未命名列表,则列表索引将用作 id。

if(is.null(names(my.lst))){
  names(my.lst) <- c(1:length(my.lst))
}

df <- do.call(rbind, lapply(seq_along(my.lst), function(ids, vals, i){
                              tdf <- as.data.frame(vals[[i]][c(1:2)])
                              tdf$id <- ids[[i]]
                              return(tdf[, c('id','states','times')])
                            }, vals = my.lst, ids = names(my.lst)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2013-03-20
    • 2021-12-08
    • 2015-06-27
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多