I am struggling for a few days with a solution myself. Hope you can help. I checked the following already:
Counting the number of elements with the values of x in a vector
Split strings in a matrix column and count the single elements in a new vector
I have a dataframe as follows:
df<-list(column=c("apple juice,guava-peach juice,melon apple juice","orange juice,pineapple strawberry lemon juice"))
df<-data.frame(df)
I want to separate each element separated by "," in its own column. Number of columns must be based on the maximum number of elements in each row in column
column1 column2 column3
apple juice guava-peach juice melon apple juice
orange juice pineapple strawberry lemon juice NA
I tried using
library(tidyverse)
library(stringr)
#want to calculate number of columns needed and the sequence
x<-str_count(df$column)
results<-df%>%separate(column,x,",")
Unfortunately I am not getting what I wish to. Thank you for your help.