首页 > 解决方案 > 在列表中汇总结果

问题描述

我想知道如何创建所有状态的列及其相应的时间(每个列表对应一个 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

在此处输入图像描述

标签: rlistdataframemarkov-models

解决方案


以下应返回包含 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)))

推荐阅读