我试图使我对Reshape()的了解适应我的需要,但我不能。 我的data.frame有两组列(a和b),我想分别将其整形为长格式。 它还具有我要保持不变的变量。像这样:
id 2010a 2011a 2012a char 2010b 2011b 2012b
1 1 2 3 x 5 6 7
2 1 2 3 y 5 6 7
3 1 2 3 z 5 6 7
4 1 2 3 x 5 6 7
采用这种长格式
id year a b char
1 2010 1 5 x
2 2010 1 5 y
3 2010 1 5 z
4 2010 1 5 x
1 2011 2 6 x
2 2011 2 6 y
3 2011 2 6 z
4 2011 2 6 x
1 2012 3 7 x
2 2012 3 7 y
3 2012 3 7 z
4 2012 3 7 x
谢谢!
答案 0 :(得分:2)
使用tidyr
的解决方案:
library(tidyr)
library(dplyr)
dt_final <- gather(dt_initial, key = year, value = value, -id) %>%
separate(col=year, into=c("year", "name"), sep=-1) %>%
spread(key = name, value = value) %>%
arrange(id, year)
答案 1 :(得分:1)
那呢?
library(data.table)
data2 <- melt(setDT(data), id.vars = "id", variable.name = "year")
data2[, l := substr(year, 6,6)][, year := gsub("[a-zA-Z]", "", year)]
dcast(data2, id + year ~ l, value.var = "value")[order(year, id)]
id year a b
1: 1 2010 1 5
2: 2 2010 1 5
3: 3 2010 1 5
4: 4 2010 1 5
5: 1 2011 2 6
6: 2 2011 2 6
7: 3 2011 2 6
8: 4 2011 2 6
9: 1 2012 3 7
10: 2 2012 3 7
11: 3 2012 3 7
12: 4 2012 3 7
数据:
data <- data.frame(
id = 1:4,
`2010a` = c(1L, 1L, 1L, 1L),
`2011a` = c(2L, 2L, 2L, 2L),
`2012a` = c(3L, 3L, 3L, 3L),
`2010b` = c(5L, 5L, 5L, 5L),
`2011b` = c(6L, 6L, 6L, 6L),
`2012b` = c(7L, 7L, 7L, 7L)
)