1

How can you convert a data frame column filled with nested list to text strings but keep the the list element names?

I’ve tried:

data_frame$column_new <- paste(unlist(data_frame$column), collapse = “”)

It works but creates a string for each list in corresponding row that doesn’t retain the list element names, it’s almost there.

The list looks like:

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

next row ..$ :list of 1 .. ..$ list of 6 .. .. ..$ apple : chr "small" .. .. ..$ pear : int 3 .. .. ..$ orange: list() .. .. ..$ grape: list of 2

I want the result put back into the data frame cell:

So currently it looks like this:

data_frame$column
list
list
“”

To....

data_frame$column  new_column_string
list               new text string of every thing in list (with names kept)
list               new text string of list “”

Almost there with his now,

With

    <- sapply(data_frame$colum_string, 
                 function(x) paste(unlist(x, use.names = TRUE), collapse = “ “))

But does not keep the list element names!!

Mikey
  • 165
  • 1
  • 14

1 Answers1

0
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
Aurèle
  • 12,545
  • 1
  • 31
  • 49
  • Think there is confusion, I meant a full text string e.g ‘apple = ‘1 next row ‘pear = 3’ not a numeric – Mikey Aug 02 '18 at 13:01
  • So new column would be contain ‘apple = 1’ sometimes this can be a character too e.g ‘pear = new’ – Mikey Aug 02 '18 at 13:04
  • This would fail if the data frame was data_frame <- data.frame(column = I(list( list(list(apple = "large", pear = "small")), list(banana = "tiny"), list(list(kiwi = "fat", list(orange = "huge"))) )), id = 1:3) – Mikey Aug 02 '18 at 13:13
  • "Error in mutate_impl(.data, dots): evaluation error: column name must be a 1d atomic vector or a list" - I think it might be because some rows are null., and some list elements are null, can it handle / pull list names where empty? / rows are empty? – Mikey Aug 02 '18 at 13:26
  • Don't need the ID also, e.g apple = "large", pear = "small would be in single string, new_column_string =" apple = large, pear = small" on one row – Mikey Aug 02 '18 at 13:43
  • Can you please edit your question with a representative (possibly simulated) sample of your data, and with the corresponding desired output, in a copy-paste ready manner (e.g. using `dput`)? – Aurèle Aug 02 '18 at 13:56
  • Thanks for editing your question. Though it's not practical yet. Run `dput(head(data_frame, 10))` and copy-paste the output – Aurèle Aug 02 '18 at 14:05