Long to wide data with tidyR?

dcast in data.table v1.9.5+ can handle multiple value.var columns. Thus we can do:

require(data.table) # v1.9.5+
dcast(setDT(df), name ~ group, value.var=c("V1", "V2"))
#    name V1_g1 V1_g2 V2_g1 V2_g2
# 1:    A    10    40     6     3
# 2:    B    20    30     1     7

Basically, no need to melt and cast, rather directly cast. You can install it by following these instructions.


Since tidyr 1.0.0 you can do the following:

library(tidyr)

df = data.frame(name=c("A","A","B","B"),
                group=c("g1","g2","g1","g2"),
                V1=c(10,40,20,30),
                V2=c(6,3,1,7))

pivot_wider(df, names_from = "group", values_from = c("V1", "V2"), names_sep = ".")
#> # A tibble: 2 x 5
#>   name  V1.g1 V1.g2 V2.g1 V2.g2
#>   <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 A        10    40     6     3
#> 2 B        20    30     1     7

Created on 2019-09-14 by the reprex package (v0.3.0)


The reshape code is compact as it works for multiple value columns. Using the same in tidyr, may need a couple of steps. Convert the 'wide' format to 'long' using gather so that there will be a single 'Val' column, unite the 'Var' (from previous step) and 'group' columns to create a single 'VarG' column, and then use spread to reconvert the 'long' to 'wide' format.

library(tidyr)
gather(df, Var, Val, V1:V2) %>% 
                unite(VarG, Var, group) %>% 
                spread(VarG, Val)
#    name V1_g1 V1_g2 V2_g1 V2_g2
#1    A    10    40     6     3
#2    B    20    30     1     7

Tags:

R

Tidyr