1

I have a dataframe that looks roughly like this:

   type       items
1  fruit      "apple, banana, orange"
2  vegetable  "tomato, carrot"

Note that each row in items is one value and not split by a delimiter. I need to convert each value in items to a single list, where each item is an independent value:

["apple", "banana", "orange", "tomato", "carrot"]

How can I acheive this?

hirshg
  • 155
  • 7

1 Answers1

1

We can use paste with collapse = ", " or the conveninent function toString to create a single string

toString(df1$items)

and if it needs to be a vector of values, wrap with scan

scan(text = toString(df1$items), what ="", sep =",",
        strip.white = TRUE, quiet = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662