将每 n 列转置为 R 中的新行

Transpose every n columns into new rows in R

我有一个看起来像这样的数据框

     Frame RightEye_x RightEye_y RightEye_z LeftEye_x LeftEye_y LeftEye_z
     0       773        490        0         778       322          0
     1       780        490        0         789       334          0
     2       781        490        0         792       334          0
     3       783        337        0         797       334          1

我想把它改造成

BodyPart Frame  x         y         z 
RightEye   0   773       490        0 
RightEye   1   780       490        0
RightEye   2   781       490        0 
RightEye   3   783       337        0 
LeftEye    0   778       322        0
LeftEye    1   789       334        0
LeftEye    2   792       334        0   
LeftEye    3   797       334        1    
             

我们可以像下面这样使用基数 R reshape

reshape(
    setNames(df, gsub("(.*)_(.*)", "\2_\1", names(df))),
    direction = "long",
    idvar = "Frame",
    varying = -1,
    timevar = "BodyPart",
    sep = "_"
)

这给出了

           Frame BodyPart   x   y z
0.RightEye     0 RightEye 773 490 0
1.RightEye     1 RightEye 780 490 0
2.RightEye     2 RightEye 781 490 0
3.RightEye     3 RightEye 783 337 0
0.LeftEye      0  LeftEye 778 322 0
1.LeftEye      1  LeftEye 789 334 0
2.LeftEye      2  LeftEye 792 334 0
3.LeftEye      3  LeftEye 797 334 1

使用 data.table 中的 melt(...) 方法:

library(data.table)
setDT(df)
result <- melt(df, measure.vars = patterns(c('_x', '_y', '_z')), value.name = c('x', 'y', 'z'))
result[, variable:=c('RightEye', 'LeftEye')[variable]]
result
##    Frame variable   x   y z
## 1:     0 RightEye 773 490 0
## 2:     1 RightEye 780 490 0
## 3:     2 RightEye 781 490 0
## 4:     3 RightEye 783 337 0
## 5:     0  LeftEye 778 322 0
## 6:     1  LeftEye 789 334 0
## 7:     2  LeftEye 792 334 0
## 8:     3  LeftEye 797 334 1