2

I got the following list for my model:

List of 9
 $ phi           : num [1:5, 1:1500] 1.8e-04 1.8e-04 1.8e-04 1.8e-04 1.8e-04 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:5] "t_1" "t_2" "t_3" "t_4" ...
  .. ..$ : chr [1:1500] "word1" "word2" "word3" "word4" ...
 $ theta         : num [1:500, 1:5] 0.1234 0.4567 0.01234 0.04567 0.02345 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:500] "1" "2" "3" "4" ...
  .. ..$ : chr [1:5] "t_1" "t_2" "t_3" "t_4" ...
 $ gamma         : num [1:5, 1:1500] 0.20 0.70 0.10 0.1 0.11 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:5] "t_1" "t_2" "t_3" "t_4" ...
  .. ..$ : chr [1:1500] "word1" "word2" "word3" "word4" ...
 $ data          :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  .. ..@ i       : int [1:10000] 1234 6789 2233 1367 1123 1123 145 145 156 1325 ...
  .. ..@ p       : int [1:1500] 0 1 2 3 4 5 6 7 8 9 ...
  .. ..@ Dim     : int [1:2] 1234 1500
  .. ..@ Dimnames:List of 2
  .. .. ..$ : chr [1:500] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:1500] "word1" "word2" "word3" "word4" ...
  .. ..@ x       : num [1:100000] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..@ factors : list()
 $ alpha         : Named num [1:5] 0.1 0.1 0.1 0.1  ...
  ..- attr(*, "names")= chr [1:5] "t_1" "t_2" "t_3" "t_4" ...
 $ beta          : Named num [1:1500] 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 ...
  ..- attr(*, "names")= chr [1:1500] "word1" "word2" "word3" "word4"

Is there a way of how to select $theta and all its attributes and save them as a data frame? In other words, I want to extract this part from the list:

$ theta         : num [1:500, 1:5] 0.1234 0.4567 0.01234 0.04567 0.02345 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:500] "1" "2" "3" "4" ...
  .. ..$ : chr [1:5] "t_1" "t_2" "t_3" "t_4" ...

and have a dataframe that looks like this (the column order does not matter):

Theta  | var1 | var2 |
0.1234 | 1    | t_1  |
0.4567 | 2    | t_2  |
0.01234| 3    | t_3  |

I have tried lapply and many other suggestions that I found in terms of list extraction but failed to extract the part shown above.

Thanks a lot!

lole_emily
  • 95
  • 9
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 11 '20 at 20:48
  • 1
    If it is a `list`, then just do `out <- lst1$theta` – akrun Jun 11 '20 at 20:48
  • 1
    What happens if you do list$theta or list[2] – Reeza Jun 11 '20 at 20:49
  • @MrFlick Unfortunately, I was not sure how to make a "reproducable" example out of the nested list that I got as an output from my LDA model. – lole_emily Jun 11 '20 at 21:14

2 Answers2

4

As already metioned in comments, you can easily access $theta with list subsetting either model$theta or model[['theta']].

$theta is a numeric matrix 500 x 5. To convert it into desirable format just melt it:

theta_matrix = model$theta
theta_df = reshape2::melt(theta_matrix, value.name = "Theta")
megamad
  • 81
  • 2
  • 1
    this was exactly what I was looking for! Thank you very much! – lole_emily Jun 11 '20 at 21:15
  • 1
    Nice answer, megamad - upvoted. I didn't get that the OP wanted a length 1500 dataframe in this format - I thought it was maybe a misunderstanding of the structure of the list, but it turns out I just misunderstood the question! – Allan Cameron Jun 11 '20 at 21:21
1

Note that theta is a 500 x 5 numeric matrix, so the data frame will have 500 rows and 5 columns named t_1, t_2, t_3, t_4 and t_5 - it won't be a 3 - row data frame as in your expected output.

Assuming your list is called my_list, here's how you get theta as a data frame:

as.data.frame(my_list$theta)

Or perhaps

setNames(as.data.frame(my_list$theta), attr(my_list$theta, "dimnames")[[2]])
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • hey allan! thank you so much - that was really helpful. I guess I was confused about the whole lapply and list referencing topic that I did not think of just trying to reference to the list element by "list_name$theta." For this example I used the df as described by megamad. However, your answer was also very helpful since I need the version you created for other variables. – lole_emily Jun 11 '20 at 21:19