【发布时间】:2019-01-09 21:49:35
【问题描述】:
如何将填充了嵌套列表的数据框列转换为文本字符串,但保留列表元素名称?
我试过了:
data_frame$column_new <- paste(unlist(data_frame$column), collapse = “”)
它可以工作,但会为相应行中的每个列表创建一个不保留列表元素名称的字符串,它几乎就在那里。
列表如下:
data_frame10000 obs. of 1 variable
column: list of 10000
..$ :list of 1
.. ..$ list of 6
.. .. ..$ apple : chr "small"
.. .. ..$ pear : int 3
.. .. ..$ orange: list()
.. .. ..$ grape: list of 2
下一行 ..$ : 列表 1 .. ..$ 清单 6 .. .. ..$ apple : chr "small" .. .. ..$ 梨:int 3 .. .. ..$ 橙色:list() .. .. ..$ 葡萄:2 个列表
我希望将结果放回数据框单元格:
所以目前看起来是这样的:
data_frame$column
list
list
“”
到....
data_frame$column new_column_string
list new text string of every thing in list (with names kept)
list new text string of list “”
他现在差不多了,
与
<- sapply(data_frame$colum_string,
function(x) paste(unlist(x, use.names = TRUE), collapse = “ “))
但不保留列表元素名称!!
【问题讨论】:
-
您能否举一个具有预期输出的命名嵌套列表的示例?
-
也许没有
paste。这将保留名称:column <- list(list(apple = 1L, pear = 3L)); unlist(column)。 -
问题是我不知道列表里面的名字是什么,能不知道就转成字符串吗?
-
请以易于粘贴的方式提供您的数据,例如使用
dput。