重塑整形包中的数据集

时间:2014-11-12 22:11:44

标签: r reshape

我正在尝试使用Reshape包重塑我的数据。我很难重塑它。如果你能帮助我,我将非常感激。

数据如下:

structure(list(ID = 1:3, group = 1:3, v1 = c(1L, 1L, 1L), v2 = c(1L, 1L, 0L), v1.1 = 1:3, v2.1 = c(1L, 1L, 1L), v1.2 = c(1L, 0L, 1L ), v2.2 = c(0L, 1L, 1L), v1.3 = c(1L, 1L, 1L), v2.3 = c(1L, 1L, 1L)), .Names = c("ID", "group", "v1", "v2", "v1.1", "v2.1", "v1.2", "v2.2", "v1.3", "v2.3"), class = "data.frame", row.names = c(NA, -3L))

ID  group      X1              X2              X3          X4   
ID  group   v1  v2  v1  v2  v1  v2  v1  v2
1   1       1   1   1   1   1   0   1   1
2   2   1   1   2   1   0   1   1   1
3   3   1   0   3   1   1   1   1   1

我想要这样的数据。非常感谢您的帮助

ID  group   X   v1  v2
1   1   1   1   1
1   1   2   1   1
1   1   3   1   0
1   1   4   1   1
2   2   1   1   1
2   2   2   2   1
2   2   3   0   1
2   2   4   1   1
3   3   1   1   0
3   3   2   3   1
3   3   3   1   1
3   3   4   1   1

3 个答案:

答案 0 :(得分:2)

这似乎有效。

df     <- structure(list(ID = 1:3, group = 1:3, v1 = c(1L, 1L, 1L), v2 = c(1L, 1L, 0L), v1.1 = 1:3, v2.1 = c(1L, 1L, 1L), v1.2 = c(1L, 0L, 1L ), v2.2 = c(0L, 1L, 1L), v1.3 = c(1L, 1L, 1L), v2.3 = c(1L, 1L, 1L)), .Names = c("ID", "group", "v1", "v2", "v1.1", "v2.1", "v1.2", "v2.2", "v1.3", "v2.3"), class = "data.frame", row.names = c(NA, -3L))
result <- reshape(df,idvar=1:2,
                  varying=list(c(3,5,7,9),c(4,6,8,10)),
                  timevar="X",
                  direction="long")
result <- with(result,result[order(ID,group,X),])
result

#       ID group X v1 v2
# 1.1.1  1     1 1  1  1
# 1.1.2  1     1 2  1  1
# 1.1.3  1     1 3  1  0
# 1.1.4  1     1 4  1  1
# 2.2.1  2     2 1  1  1
# 2.2.2  2     2 2  2  1
# 2.2.3  2     2 3  0  1
# 2.2.4  2     2 4  1  1
# 3.3.1  3     3 1  1  0
# 3.3.2  3     3 2  3  1
# 3.3.3  3     3 3  1  1
# 3.3.4  3     3 4  1  1

通常情况下,我建议在melt(...)包中添加reshape2,但有一组“值”列(v1v2),这可能是更快。

答案 1 :(得分:1)

您可以尝试{&#34; splitstackshape&#34;}中的merged.stack包,您可以这样申请:

library(splitstackshape)
merged.stack(
  df, var.stubs = c("v1", "v2"), 
  sep = "var.stubs")[, .time_1 := NULL][, ind := sequence(.N), 
                                        by = c("ID", "group")][]
#     ID group v1 v2 ind
#  1:  1     1  1  1   1
#  2:  1     1  1  1   2
#  3:  1     1  1  0   3
#  4:  1     1  1  1   4
#  5:  2     2  1  1   1
#  6:  2     2  2  1   2
#  7:  2     2  0  1   3
#  8:  2     2  1  1   4
#  9:  3     3  1  0   1
# 10:  3     3  3  1   2
# 11:  3     3  1  1   3
# 12:  3     3  1  1   4

或者,在同一个包中,有Reshape,它是一个试图简化基础R reshape()使用的包装器。从长远来看,它会慢于merged.stack

要使用它,首先要重命名名为&#34; v1&#34;的列。和&#34; v2&#34;到&#34; v1.0&#34;和&#34; v2.0&#34;:

setnames(df, c("v1", "v2"), c("v1.0", "v2.0"))
Reshape(df, var.stubs = c("v1", "v2"), sep = ".")
#     ID group time v1 v2
#  1:  1     1    1  1  1
#  2:  2     2    1  1  1
#  3:  3     3    1  1  0
#  4:  1     1    2  1  1
#  5:  2     2    2  2  1
#  6:  3     3    2  3  1
#  7:  1     1    3  1  0
#  8:  2     2    3  0  1
#  9:  3     3    3  1  1
# 10:  1     1    4  1  1
# 11:  2     2    4  1  1
# 12:  3     3    4  1  1

另一种选择(因为你似乎坚持使用&#34; reshape2&#34;解决方案)是首先melt数据,然后对数据进行一些修改以使其为{{1}做好准备}}

这是方法(从原始&#34; dcast&#34;数据开始,而不是我们重新命名上述列的数据):

df

你必须增加&#34;时间&#34;列,如果您想要在问题中显示的输出完全相同。

答案 2 :(得分:0)

尝试:

nddf = data.frame(ID=numeric(), group=numeric(), x=numeric(), v1=numeric(), v2=numeric())
for(i in 1:nrow(ddf)){
    nddf[nrow(nddf)+1,]=c(ddf[i,'ID'], ddf[i,'group'], 1, ddf[i,3], ddf[i,4])
    nddf[nrow(nddf)+1,]=c(ddf[i,'ID'], ddf[i,'group'], 2, ddf[i,5], ddf[i,6])
    nddf[nrow(nddf)+1,]=c(ddf[i,'ID'], ddf[i,'group'], 3, ddf[i,7], ddf[i,8])
    nddf[nrow(nddf)+1,]=c(ddf[i,'ID'], ddf[i,'group'], 4, ddf[i,9], ddf[i,10])
}
nddf
   ID group x v1 v2
1   1     1 1  1  1
2   1     1 2  1  1
3   1     1 3  1  0
4   1     1 4  1  1
5   2     2 1  1  1
6   2     2 2  2  1
7   2     2 3  0  1
8   2     2 4  1  1
9   3     3 1  1  0
10  3     3 2  3  1
11  3     3 3  1  1
12  3     3 4  1  1