Reshape: “Error: index out of bounds”

Reshape: “Error: index out of bounds”

我有一个具有以下结构的相当大的数据框:

 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")

但我给出了错误:

Error in data[, timevar] : subindex out of bounds

我还尝试了 Reshape2 库:

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

但是因为我有多个 value.var 专栏,所以我不知道该怎么做。

我们可以melt然后dcast

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

或使用 recastmelt/dcast 的包装器)来自 reshape2

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))