从列表中提取值和属性并将它们转换为 R 中的数据框
Extract values and attributes from a list and convert them into a dataframe in R
我的模型有以下列表:
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"
有没有办法 select $theta 及其所有属性并将它们保存为数据框?换句话说,我想从列表中提取这部分:
$ 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" ...
并有一个如下所示的数据框(列顺序无关紧要):
Theta | var1 | var2 |
0.1234 | 1 | t_1 |
0.4567 | 2 | t_2 |
0.01234| 3 | t_3 |
我已尝试 lapply
以及我在列表提取方面发现的许多其他建议,但未能提取上面显示的部分。
非常感谢!
请注意,theta
是一个 500 x 5 的数字矩阵,因此数据框将有 500 行和 5 列,名称为 t_1
、t_2
、t_3
, t_4
和 t_5
- 它不会像您预期的输出那样是 3 行数据框。
假设您的列表名为 my_list
,下面是如何将 theta
作为数据框:
as.data.frame(my_list$theta)
或者
setNames(as.data.frame(my_list$theta), attr(my_list$theta, "dimnames")[[2]])
正如评论中已经提到的,您可以通过列表子集 model$theta
或 model[['theta']]
.
轻松访问 $theta
$theta 是一个 500 x 5 的数字矩阵。要将其转换为所需的格式,只需将其融化即可:
theta_matrix = model$theta
theta_df = reshape2::melt(theta_matrix, value.name = "Theta")
我的模型有以下列表:
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"
有没有办法 select $theta 及其所有属性并将它们保存为数据框?换句话说,我想从列表中提取这部分:
$ 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" ...
并有一个如下所示的数据框(列顺序无关紧要):
Theta | var1 | var2 |
0.1234 | 1 | t_1 |
0.4567 | 2 | t_2 |
0.01234| 3 | t_3 |
我已尝试 lapply
以及我在列表提取方面发现的许多其他建议,但未能提取上面显示的部分。
非常感谢!
请注意,theta
是一个 500 x 5 的数字矩阵,因此数据框将有 500 行和 5 列,名称为 t_1
、t_2
、t_3
, t_4
和 t_5
- 它不会像您预期的输出那样是 3 行数据框。
假设您的列表名为 my_list
,下面是如何将 theta
作为数据框:
as.data.frame(my_list$theta)
或者
setNames(as.data.frame(my_list$theta), attr(my_list$theta, "dimnames")[[2]])
正如评论中已经提到的,您可以通过列表子集 model$theta
或 model[['theta']]
.
$theta 是一个 500 x 5 的数字矩阵。要将其转换为所需的格式,只需将其融化即可:
theta_matrix = model$theta
theta_df = reshape2::melt(theta_matrix, value.name = "Theta")