Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从具有多个id和值列的长格式转换为宽格式_R_Tidyr_Reshape2_Spread - Fatal编程技术网

R 从具有多个id和值列的长格式转换为宽格式

R 从具有多个id和值列的长格式转换为宽格式,r,tidyr,reshape2,spread,R,Tidyr,Reshape2,Spread,我一直在尝试使用多个ID和值列从宽格式转换为长格式。我更喜欢像dcast一样的tidyr解决方案,因为它默认为length 以下是我迄今为止所尝试的: df_wide <- df %>% melt(id.vars = c(Route, Address, Week)) %>% dcast(Route + Address ~ variable + Week) 下面是使用tidyr完成此操作的方法。诀窍在于,您需要首先进行收集: library(tidyr) df_

我一直在尝试使用多个ID和值列从宽格式转换为长格式。我更喜欢像dcast一样的tidyr解决方案,因为它默认为length

以下是我迄今为止所尝试的:

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

下面是使用
tidyr
完成此操作的方法。诀窍在于,您需要首先进行
收集

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
library(tidyr)
df_宽%
聚集(键,值,V1:V5)%>%
统一(“关键”,关键,周,九月=“.”)%>%
排列(键、值)
全方位
#>路线地址V1.Week1 V1.Week2 V2.Week1 V2.Week2 V3.Week1
#>1 12345_SE_法院0 1 0 0
#>2 A 33333_Neu街01 01
#>3 B 98765_西北_车道10
#>4 C 10293_西南_路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
由(v0.2.0)于2018年6月27日创建

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