重塑:“错误:索引越界”

时间:2016-04-08 10:49:54

标签: r dataframe reshape reshape2

我有一个非常大的数据框,具有以下结构:

 image       coef    v3    v4    v5    v6   ... v20
    1         A       0     1     2     3        
    1         B       2     4     6     5
    1         C       1     2     4     7
    1         D       4     5     6     4
    2         A       2     3     4     5 
    2         B       2     3     4     5 
    2         C       2     3     4     5 
    2         D       2     3     4     5 

我需要在每个图像索引的coef变量上得到“flattened”结构。现在每个图像都有形状[4:20]的变量但我需要它[1:80]与模式[A,B,C,D,A',B',C',D'.. ]。 像这样:

 image    v3    v4    v5    v6    v7    v8    v9    v10  ...   v80 
    1      0     2     1     4     1     4     2     5
    2      2     2     2     2     3     3     3     3

我试着这样做:

reshape(df, timevar = "coef", idvar = "image", direction = "wide")

但是我给了我错误:

  

data [,timevar]出错:subindex超出范围

我还尝试使用Reshape2库:

dcast(df, image~coef, value.var= )

但由于我有多个value.var列,我无法弄清楚如何做到这一点。

1 个答案:

答案 0 :(得分:1)

我们可以melt然后执行dcast

library(data.table)
dM <- melt(setDT(df1), id.var=c("image", "coef"))
dcast(dM, image~variable+coef, value.var="value")

或者使用recast

中的melt/dcastreshape2的包装器)
library(reshape2)
recast(df1, id.var=c("image", "coef"),image~variable+coef, value.var="value")
#  image v3_A v3_B v3_C v3_D v4_A v4_B v4_C v4_D v5_A v5_B v5_C v5_D v6_A v6_B v6_C v6_D
#1     1    0    2    1    4    1    4    2    5    2    6    4    6    3    5    7    4
#2     2    2    2    2    2    3    3    3    3    4    4    4    4    5    5    5    5

数据

df1 <- structure(list(image = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), 
coef = c("A", 
"B", "C", "D", "A", "B", "C", "D"), v3 = c(0L, 2L, 1L, 4L, 2L, 
2L, 2L, 2L), v4 = c(1L, 4L, 2L, 5L, 3L, 3L, 3L, 3L), v5 = c(2L, 
6L, 4L, 6L, 4L, 4L, 4L, 4L), v6 = c(3L, 5L, 7L, 4L, 5L, 5L, 5L, 
5L)), .Names = c("image", "coef", "v3", "v4", "v5", "v6"), 
class = "data.frame", row.names = c(NA, -8L))