汇总结果列表

时间:2019-02-22 18:03:37

标签: r list dataframe markov-models

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

1 个答案:

答案 0 :(得分:0)

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