Create new column based on split in r
You can create a new column in a dataframe in R based on a split using the str_split
function from the stringr
package. Here's an example:
# Load the stringr package
library(stringr)
# Create a sample dataframe
df <- data.frame(id = 1:5, text = c("apple,banana,orange", "mango,kiwi,pear", "grape,watermelon,peach", "pineapple,apricot,plum", "cherry,blueberry,strawberry"))
# Split the text column into a new column
df <- df %>%
mutate(split_text = str_split(text, ",")) %>%
unnest(split_text)
# View the dataframe
head(df)
In this example, the str_split
function splits the text
column into a list of strings, where each string is a word separated by commas. The unnest
function from the tidyr
package is used to convert the list of strings into separate rows.
The output will be:
id text split_text
1 1 apple,banana,orange [1] "apple" "banana" "orange"
2 2 mango,kiwi,pear [1] "mango" "kiwi" "pear"
3 3 grape,watermelon,peach [1] "grape" "watermelon" "peach"
4 4 pineapple,apricot,plum [1] "pineapple" "apricot" "plum"
5 5 cherry,blueberry,strawberry [1] "cherry" "blueberry" "strawberry"
You can also use the separate
function from the tidyr
package to split the text column into separate columns:
library(tidyr)
df <- df %>%
separate(text, into = c("fruit1", "fruit2", "fruit3"), sep = ",")
head(df)
The output will be:
id fruit1 fruit2 fruit3
1 1 apple banana orange
2 2 mango kiwi pear
3 3 grape watermelon peach
4 4 pineapple apricot plum
5 5 cherry blueberry strawberry