R中的嵌套列表到字符串
Nested list in R to string
如何将填充有嵌套列表的数据框列转换为文本字符串但保留列表元素名称?
我试过:
data_frame$column_new <- paste(unlist(data_frame$column), collapse = “”)
它可以工作,但会为相应行中的每个列表创建一个不保留列表元素名称的字符串,它几乎就在那里。
列表如下:
data_frame
10000 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
.. .. ..$苹果:chr "small"
.. .. ..$梨:int 3
.. .. ..$ 橙色:列表()
.. .. ..$ 葡萄:列表 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 = “ “))
但不保留列表元素名称!!
data_frame <- data.frame(column = I(list(
list(list(apple = "large", pear = "small")),
list(banana = "tiny"),
list(list(kiwi = "fat", list(orange = "huge")))
)))
data_frame$new_column_string <-
vapply(data_frame$column, function(.x) {
y <- unlist(.x, use.names = TRUE)
paste(names(y), y, sep = ' = ', collapse = ', ')
}, FUN.VALUE = character(1))
data_frame
# column new_column_string
# 1 list(app.... apple = large, pear = small
# 2 tiny banana = tiny
# 3 list(kiw.... kiwi = fat, orange = huge
如何将填充有嵌套列表的数据框列转换为文本字符串但保留列表元素名称?
我试过:
data_frame$column_new <- paste(unlist(data_frame$column), collapse = “”)
它可以工作,但会为相应行中的每个列表创建一个不保留列表元素名称的字符串,它几乎就在那里。
列表如下:
data_frame
10000 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 .. .. ..$苹果:chr "small" .. .. ..$梨:int 3 .. .. ..$ 橙色:列表() .. .. ..$ 葡萄:列表 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 = “ “))
但不保留列表元素名称!!
data_frame <- data.frame(column = I(list(
list(list(apple = "large", pear = "small")),
list(banana = "tiny"),
list(list(kiwi = "fat", list(orange = "huge")))
)))
data_frame$new_column_string <-
vapply(data_frame$column, function(.x) {
y <- unlist(.x, use.names = TRUE)
paste(names(y), y, sep = ' = ', collapse = ', ')
}, FUN.VALUE = character(1))
data_frame
# column new_column_string
# 1 list(app.... apple = large, pear = small
# 2 tiny banana = tiny
# 3 list(kiw.... kiwi = fat, orange = huge