raquel
raquel

Reputation: 121

Split values in a column synced to all other columns

I want to separate values in column c where there are >1 value. I have found that from tidyr you can use separate_rows but I can't make it work. Happy for suggestions to solve it!

a <- c("e","f","g")
b <- c(2,1,3)
c <- c(27,4,678)
df <- data.frame(a,b,c)
df
 a b   c
 e 2  27
 f 2   4
 g 2 678

I want it to look like:

a b c
 e 2 2
 e 2 7
 f 1 4
 g 3 6
 g 3 7
 g 3 8

I have tried the following code from similar example but it doesn't work

library(tidyr)
separate_rows(df,c, convert=TRUE)

Upvotes: 4

Views: 54

Answers (2)

Sotos
Sotos

Reputation: 51592

To do it via separate_rows you need to modify your delimiter, i.e.

df %>% 
 separate_rows(c, sep = '(?<=.)(?=.)')

#  a b c
#1 e 2 2
#2 e 2 7
#3 f 1 4
#4 g 3 6
#5 g 3 7
#6 g 3 8

Or for your new data frame,

df %>% 
 separate_rows(c, sep = '(?<=.)(?=.)')

#  a b c  d  e
#1 e 2 2 45  4
#2 e 2 7 45  4
#3 f 2 4  6 78
#4 g 2 6 78 92
#5 g 2 7 78 92
#6 g 2 8 78 92

Upvotes: 0

tmfmnk
tmfmnk

Reputation: 39858

One dplyr and tidyr possibility could be:

df %>%
 mutate(c = strsplit(as.character(c), "")) %>%
 unnest()

  a b c
1 e 2 2
2 e 2 7
3 f 2 4
4 g 2 6
5 g 2 7
6 g 2 8

And probably you want to transform it back into a numeric vector:

df %>%
 mutate(c = strsplit(as.character(c), "")) %>%
 unnest() %>%
 mutate(c = as.numeric(c))

Upvotes: 5

Related Questions