我有一个如下所示的数据框:
A1 A2 A3 A4 A5 A6 B1 B2 B3 B4 B5 B6
A7 A8 A9 A10 A11 A12 B7 B8 B9 B10 B11 B12
A13 A14 A15 A16 A17 A18 B13 B14 B15 B16 B17 B18
A19 A20 A21 A22 A23 A24 B19 B20 B21 B22 B23 B24
A25 A26 A27 A28 A29 A30 B25 B26 B27 B28 B29 B30
A31 A32 A33 A34 A35 A36 B31 B32 B33 B34 B35 B36
A37 A38 A39 A40 A41 A42 B37 B38 B39 B40 B41 B42
A43 A44 A45 A46 A47 A48 B43 B44 B45 B46 B47 B48
A49 A50 A51 A52 A53 A54 B49 B50 B51 B52 B53 B54
A55 A56 A57 A58 A59 A60 B55 B56 B57 B58 B59 B60
A61 A62 A63 A64 A65 A66 B61 B62 B63 B64 B65 B66
A67 A68 A69 A70 A71 A72 B67 B68 B69 B70 B71 B72
A73 A74 A75 A76 A77 A78 B73 B74 B75 B76 B77 B78
A79 A80 A81 A82 A83 A84 B79 B80 B81 B82 B83 B84
A85 A86 A87 A88 A89 A90 B85 B86 B87 B88 B89 B90
A91 A92 A93 A94 A95 A96 B91 B92 B93 B94 B95 B96
数据集只有16行,但有数百列。有意义的块总是6列宽。我想重组数据框,使它看起来像这样:
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17 A18 A19 A20 A21 A22 A23 A24 A25 A26 A27 A28 A29 A30 A31 A32 A33 A34 A35 A36 A37 A38 A39 A40 A41 A42 A43 A44 A45 A46 A47 A48 A49 A50 A51 A52 A53 A54 A55 A56 A57 A58 A59 A60 A61 A62 A63 A64 A65 A66 A67 A68 A69 A70 A71 A72 A73 A74 A75 A76 A77 A78 A79 A80 A81 A82 A83 A84 A85 A86 A87 A88 A89 A90 A91 A92 A93 A94 A95 A96
B1 B2 B3 B4 B5 B6 B7 B8 B9 B10 B11 B12 B13 B14 B15 B16 B17 B18 B19 B20 B21 B22 B23 B24 B25 B26 B27 B28 B29 B30 B31 B32 B33 B34 B35 B36 B37 B38 B39 B40 B41 B42 B43 B44 B45 B46 B47 B48 B49 B50 B51 B52 B53 B54 B55 B56 B57 B58 B59 B60 B61 B62 B63 B64 B65 B66 B67 B68 B69 B70 B71 B72 B73 B74 B75 B76 B77 B78 B79 B80 B81 B82 B83 B84 B85 B86 B87 B88 B89 B90 B91 B92 B93 B94 B95 B96
理想情况下,成品将是96列宽,每个“有意义的数据块”都有一行。
欢迎任何/所有建议!
答案 0 :(得分:2)
一种方法:
rbind( c ( t ( dfrm[1:6])), # t will turn rows into columns of matrix
c ( t ( dfrm[7:12])) ) # c() will concatenate the columns
根据您对此的要求,可能需要在结果周围包裹as.data.frame
,因为它会返回一个矩阵。
示例已解决,现在要求提供更通用的解决方案:
t( sapply(seq(1, length(dfrm), by=6), function(n) c(t(dfrm[n:(n+5)])) ) )
#---
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "A13"
[2,] "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "B9" "B10" "B11" "B12" "B13"
[,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
[1,] "A14" "A15" "A16" "A17" "A18" "A19" "A20" "A21" "A22" "A23" "A24" "A25"
[2,] "B14" "B15" "B16" "B17" "B18" "B19" "B20" "B21" "B22" "B23" "B24" "B25"
[,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37]
[1,] "A26" "A27" "A28" "A29" "A30" "A31" "A32" "A33" "A34" "A35" "A36" "A37"
[2,] "B26" "B27" "B28" "B29" "B30" "B31" "B32" "B33" "B34" "B35" "B36" "B37"
[,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49]
[1,] "A38" "A39" "A40" "A41" "A42" "A43" "A44" "A45" "A46" "A47" "A48" "A49"
[2,] "B38" "B39" "B40" "B41" "B42" "B43" "B44" "B45" "B46" "B47" "B48" "B49"
[,50] [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61]
[1,] "A50" "A51" "A52" "A53" "A54" "A55" "A56" "A57" "A58" "A59" "A60" "A61"
[2,] "B50" "B51" "B52" "B53" "B54" "B55" "B56" "B57" "B58" "B59" "B60" "B61"
[,62] [,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72] [,73]
[1,] "A62" "A63" "A64" "A65" "A66" "A67" "A68" "A69" "A70" "A71" "A72" "A73"
[2,] "B62" "B63" "B64" "B65" "B66" "B67" "B68" "B69" "B70" "B71" "B72" "B73"
[,74] [,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85]
[1,] "A74" "A75" "A76" "A77" "A78" "A79" "A80" "A81" "A82" "A83" "A84" "A85"
[2,] "B74" "B75" "B76" "B77" "B78" "B79" "B80" "B81" "B82" "B83" "B84" "B85"
[,86] [,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96]
[1,] "A86" "A87" "A88" "A89" "A90" "A91" "A92" "A93" "A94" "A95" "A96"
[2,] "B86" "B87" "B88" "B89" "B90" "B91" "B92" "B93" "B94" "B95" "B96"
答案 1 :(得分:2)
我选择1)将您的数据转换为三维数组2)置换维度3)恢复为二维数组:
a <- as.matrix(df)
n1 <- nrow(a)
n2 <- ncol(a)
dim(a) <- c(n1, 6, n2 / 6)
a <- aperm(a, c(3, 2, 1))
dim(a) <- c(n2 / 6, 6 * n1)