-1

我被困在尝试使用多个 ID 和值列从宽格式转换为长格式。我更喜欢 tidyr 解决方案,因为 dcast 默认为长度。

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

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                        
4

1 回答 1

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 包(v0.2.0) 于 2018 年 6 月 27 日创建。

于 2018-06-27T05:37:07.707 回答