I have my df1, where A and B column has same uniqe entry. C and D column may have different entries as shown. I like to trasform it to df2, where C column is expanded to C1,C2,C3 and same as D column.
I try to use pivot_wider to make it wider, but seems it does not work well in this case. Any better way to do that using dplyr tools? Thanks in advance.
library(tibble)
df1<-tribble(
~A,~B,~C,~D,
4,1,2,7,
4,1,3,9,
4,1,4,10
)
df1
#> # A tibble: 3 x 4
#> A B C D
#> <dbl> <dbl> <dbl> <dbl>
#> 1 4 1 2 7
#> 2 4 1 3 9
#> 3 4 1 4 10
# Transform df1 to df2
df2<-tribble(
~A,~B,~C1,~C2,~C3,~D1,~D2,~D3,
4,1,2,3,4,7,9,10
)
df2
#> # A tibble: 1 x 8
#> A B C1 C2 C3 D1 D2 D3
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 4 1 2 3 4 7 9 10