我有一个数据集,其中包含一个(或多个)id 变量和许多具有相同结构的变量子集。我想以长格式堆叠这些子集:我的示例中有三个子集,因此最终表的行数必须是原来的三倍,并且 id 变量必须是三倍。
该foo_data
表是我在 R 中的数据示例:
individuals <-c("individual1","individual2","individual3")
subset1.var1 <- c("value1","value2","value3")
subset1.var2 <- c("value4","value5","value6")
subset1.var3 <- c("value7","value8","value9")
subset2.var1 <- c("value10","value11","value12")
subset2.var2 <- c("value13","value14","value15")
subset2.var3 <- c("value16","value17","value18")
subset3.var1 <- c("value19","value20","value21")
subset3.var2 <- c("value22","value23","value24")
subset3.var3 <- c("value25","value26","value27")
foo_data <-data.frame(individuals,subset1.var1,subset1.var2,subset1.var3,subset2.var1,
subset2.var2,subset2.var3,subset3.var1,subset3.var2,subset3.var3)
foo_data
这就是我想要的:
structure(list(id = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L), .Label = c("individual1", "individual2", "individual3"), class = "factor"),
var1 = structure(1:9, .Label = c("value1", "value2", "value3",
"value10", "value11", "value12", "value19", "value20", "value21"
), class = "factor"), var2 = structure(1:9, .Label = c("value4",
"value5", "value6", "value13", "value14", "value15", "value22",
"value23", "value24"), class = "factor"), var3 = structure(1:9, .Label = c("value7",
"value8", "value9", "value16", "value17", "value18", "value25",
"value26", "value27"), class = "factor")), .Names = c("id",
"var1", "var2", "var3"), row.names = c(NA, 9L), class = "data.frame")
# individuals time var1 var2 var3
# individual1.subset1 individual1 subset1 value1 value4 value7
# individual2.subset1 individual2 subset1 value2 value5 value8
# individual3.subset1 individual3 subset1 value3 value6 value9
# individual1.subset2 individual1 subset2 value10 value13 value16
# individual2.subset2 individual2 subset2 value11 value14 value17
# individual3.subset2 individual3 subset2 value12 value15 value18
# individual1.subset3 individual1 subset3 value19 value22 value25
# individual2.subset3 individual2 subset3 value20 value23 value26
# individual3.subset3 individual3 subset3 value21 value24 value27
最明显的解决方案是计算一个循环,如下所示:
stacked_foo <- foo_data[,c(1:4)]
noms <- c("id","var1","var2","var3")
names(stacked_foo) <- noms
for (i in 2:3){
c(1,(3*i-1):(3*(i+1)-2))
temp <- foo_data[,c(1,(3*i-1):(3*(i+1)-2))]
names(temp) <- noms
stacked_foo <- rbind(stacked_foo,temp)
print(i)
}
但是,难道没有像melt
or这样的内置函数更快速、更简洁的东西stack
吗?