简体   繁体   中英

Long to wide data with tidyR?

I have data that looks something like this

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))

I want to reshape it to look like this:

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

Is it possible to do it with tidyR?

I can do it with reshape

reshape(df, idvar='name', timevar='group', direction='wide')

but is always good to learn something new.

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

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)

df1 %>%
    gather(!c("name","group"), key="var" ,value="value") %>%
    unite(col='temp', c('var','group'), sep='.') %>%
    spread(temp, value=value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM