Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

I would like to split one column into two within at data frame based on a delimiter. For example,

a|b

b|c

to become

a    b

b    c

within a data frame.

Thanks!

1 Answer

0 votes
by

To split the columns at a delimiter, you can use the separate function from the tidyr package as depicted in the following example:

df <- data.frame(ID=1:3, Fun=c('a|b','b|c','x|y'))

>  df

  ID Fun

1  1 a|b

2  2 b|c

3  3 x|y

 library(tidyr)

 

 df %>%

   

   separate(col =Fun,into= c("foo", "bar"), sep = "\\|")

 

Output:

  ID foo bar

1  1   a   b

2  2   b   c

3  3   x   y

Browse Categories

...