For example I got :
V1 <- c("apple + orange + banana","apple+green + orange + banana")
#careful, there is no space in "apple+green"
data <- data.frame(V1)
V1
1 apple + orange + banana
2 apple+green + orange + banana
And I would like this kind of dataframe :
data_final
V1 V2 V3
1 apple orange banana
2 apple+green orange banana
I tried with strsplit :
strsplit(as.character(data$V1), ' +', fixed=TRUE)
I added a space because of the string "apple+green". But the result gave me a column composed by a list. And I want a dataframe
Could you please help me?
You can also try this with splitstackshape package:
library(splitstackshape)
x <- cSplit(data, 'V1', ' + ')
Result:
V1_1 V1_2 V1_3
1: apple orange banana
2: apple+green orange banana
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With