-3

i would like to know how to create columns of all states and their corresponding time(each list correspond to an id).The Qmatrix is not important as it remains the same.

 ``$ :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

I want it to be in this form:

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

bbStudent
  • 35
  • 1
  • 4
  • 1
    Please provide an example output data.frame as well so we know what the final output should look like – Andrew Taylor Feb 22 '19 at 18:04
  • Thank you @AndrewTaylor . I have edited my question and showed the way I way I need the data. Grateful if you could help me – bbStudent Feb 22 '19 at 19:09
  • I'm sorry, but from the list and our example output, I'm not seeing the logic. Can you please explain how you got from the list to the output. Also, we need to be input data (your list) to be a part of a reproducible example. Please make a test dataset, or dput() your list and add it to the post. – Andrew Taylor Feb 22 '19 at 20:50

1 Answers1

0

The following should return a dataframe containing columns for ids, states, and times. The ids will be based on the list item names. If an unnamed list is used, then the list index will be used as the 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)))
Matt Jewett
  • 3,249
  • 1
  • 14
  • 21