0

I need to separate one column but not into multiple columns rather than I need new rows with duplicate values.

Some data

library(tidyverse)
d <- tibble(V1=1:6, V2=c(paste(letters[1:5], letters[6:10], sep=", "), "g"))
d
# A tibble: 6 × 2
     V1    V2
  <int> <chr>
1     1  a, f
2     2  b, g
3     3  c, h
4     4  d, i
5     5  e, j
6     6     g

Separating V2 into new columns is straightforward using tidyr.

d  %>% separate(V2, c("V3", "V4"), ",") 

But in the end I want something like this.

V1   V2
1     a     
1     f
2     b   
2     g
3     c
3     h
4     d
4     i
5     e
5     j
6     g

Tried to join in the end or to bind the rows using this code but without success:

d  %>% separate(V2, c("V3", "V4"), ",") %>% 
       select(1,2) %>% bind_rows(select(1,3))
Axeman
  • 32,068
  • 8
  • 81
  • 94
Roman
  • 17,008
  • 3
  • 36
  • 49

0 Answers0