从具有多个id和value列的long转换为宽格式

时间:2018-06-27 05:28:57

标签: r tidyr reshape2 spread

我一直在尝试从具有多个ID和值列的宽格式转换为长格式。我希望将tidyr解决方案作为dcast使用,因为默认设置为length。

这是我到目前为止尝试过的:

df_wide <- df %>%
    melt(id.vars = c(Route, Address, Week)) %>%
    dcast(Route + Address ~ variable + Week)

数据:

df <- read.table(text = "
    Route    Week    Address    V1    V2    V3    V4    V5
    A    Week1    12345_SE_Court    0    1    0    0    0
    A    Week2    12345_SE_Court    0    0    1    1    1
    B    Week1    98765_NW_Drive    1    1    0    0    1
    B    Week2    98765_NW_Drive    0    1    0    1    0
    C    Week1    10293_SW_Road     0    0    0    0    1
    C    Week2    10293_SW_Road     1    0    0    0    1
    A    Week1    33333_NE_Street   0    1    1    0    0
    A    Week2    33333_NE_Street   1    0    1    0    0"
    , header = TRUE)

所需的输出:

Route    Address    V1.Week1    V2.Week1    V3.Week1    V4.Week1    V5.Week1    V1.Week1    V2.Week2    V3.Week2    V4.Week2    V5.Week2
A    12345_SE_Court    0           1           0          0            0           0           0           1           1           1
A    33333_NE_Street   0           1           1          0            1           0           1           0           0           0
B    98765_NW_Drive    1           1           0          0            1           0           1           0           1           0              
C    10293_SW_Road     0           0           0          0            1           1           0           0           0           1                        

1 个答案:

答案 0 :(得分:2)

这是使用tidyr进行此操作的方法。诀窍是您需要先进行gather

library(tidyr)
df_wide <- df %>%
  gather(key, value, V1:V5) %>%
  unite("key", key, Week, sep = ".") %>%
  spread(key, value)

df_wide
#>   Route         Address V1.Week1 V1.Week2 V2.Week1 V2.Week2 V3.Week1
#> 1     A  12345_SE_Court        0        0        1        0        0
#> 2     A 33333_NE_Street        0        1        1        0        1
#> 3     B  98765_NW_Drive        1        0        1        1        0
#> 4     C   10293_SW_Road        0        1        0        0        0
#>   V3.Week2 V4.Week1 V4.Week2 V5.Week1 V5.Week2
#> 1        1        0        1        0        1
#> 2        1        0        0        0        0
#> 3        0        0        1        1        0
#> 4        0        0        0        1        1

reprex package(v0.2.0)于2018-06-27创建。