我被困在尝试使用多个 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