0 votes
1 view
in R Programming by (6.5k 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 (25.3k points)

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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...