将data.frame matrix-column转换为列

时间:2017-11-22 18:08:00

标签: r dataframe matrix

aggregate与复合函数一起使用时,生成的data.frame在列内有矩阵。

ta=aggregate(cbind(precision,result,prPo)~rstx+qx+laplace,t0
              ,function(x) c(x=mean(x),m=min(x),M=max(x)))
ta=head(ta)
dput(ta)
structure(list(rstx = c(3, 3, 2, 3, 2, 3), qx = c(0.2, 0.25, 
0.3, 0.3, 0.33, 0.33), laplace = c(0, 0, 0, 0, 0, 0), precision = structure(c(0.174583333333333, 
0.186833333333333, 0.3035, 0.19175, 0.30675, 0.193666666666667, 
0.106, 0.117, 0.213, 0.101, 0.22, 0.109, 0.212, 0.235, 0.339, 
0.232, 0.344, 0.232), .Dim = c(6L, 3L), .Dimnames = list(NULL, 
    c("x", "m", "M"))), result = structure(c(-142.333333333333, 
-108.316666666667, -69.1, -85.7, -59.1666666666667, -68.5666666666667, 
-268.8, -198.2, -164, -151.6, -138.2, -144.8, -30.8, -12.2, -14.2, 
-3.8, -12.6, -3.4), .Dim = c(6L, 3L), .Dimnames = list(NULL, 
    c("x", "m", "M"))), prPo = structure(c(3.68416666666667, 
3.045, 2.235, 2.53916666666667, 2.0775, 2.23666666666667, 1.6, 
1, 1.02, 0.54, 0.87, 0.31, 5.04, 4.02, 2.77, 3.53, 2.63, 3.25
), .Dim = c(6L, 3L), .Dimnames = list(NULL, c("x", "m", "M")))), .Names = c("rstx", 
"qx", "laplace", "precision", "result", "prPo"), row.names = c(NA, 
6L), class = "data.frame")

是否有将data.frame matrix-colum转换为列的函数?
手动地,对于每个矩阵列,列绑定和列删除都起作用:

colnames(ta)
[1] "rstx"      "qx"        "laplace"   "precision" "result"    "prPo"     
ta[,"precision"] # ta[,4]
             x     m     M
[1,] 0.1745833 0.106 0.212
[2,] 0.1868333 0.117 0.235
[3,] 0.3035000 0.213 0.339
[4,] 0.1917500 0.101 0.232
[5,] 0.3067500 0.220 0.344
[6,] 0.1936667 0.109 0.232

#column bind + column delete
ta=cbind(ta,precision=ta[,4])
ta=ta[,-4]
colnames(ta)
[1] "rstx"        "qx"          "laplace"     "result"      "prPo"        "precision.x" "precision.m"
[8] "precision.M"
ta
  rstx   qx laplace   result.x   result.m   result.M   prPo.x   prPo.m   prPo.M precision.x precision.m
1    3 0.20       0 -142.33333 -268.80000  -30.80000 3.684167 1.600000 5.040000   0.1745833       0.106
2    3 0.25       0 -108.31667 -198.20000  -12.20000 3.045000 1.000000 4.020000   0.1868333       0.117
3    2 0.30       0  -69.10000 -164.00000  -14.20000 2.235000 1.020000 2.770000   0.3035000       0.213
4    3 0.30       0  -85.70000 -151.60000   -3.80000 2.539167 0.540000 3.530000   0.1917500       0.101
5    2 0.33       0  -59.16667 -138.20000  -12.60000 2.077500 0.870000 2.630000   0.3067500       0.220
6    3 0.33       0  -68.56667 -144.80000   -3.40000 2.236667 0.310000 3.250000   0.1936667       0.109
  precision.M
1       0.212
2       0.235
3       0.339
4       0.232
5       0.344
6       0.232

2 个答案:

答案 0 :(得分:1)

matrix不支持矩阵列。因此as.matrix()data.frame转换为matrix,分解矩阵列。

这是我的想法:

library(tidyverse)

ta2 <- ta %>% 
  as.matrix() %>% 
  as.data.frame()

答案 1 :(得分:0)

在Stackoverflow的某个地方,我找到了一个非常简单的解决方案:

cbind(ta[-ncol(ta)],ta[[ncol(ta)]])
     rstx   qx laplace precision.x precision.m precision.M   result.x   result.m   result.M        x    m
1    3 0.20       0   0.1745833   0.1060000   0.2120000 -142.33333 -268.80000  -30.80000 3.684167 1.60
2    3 0.25       0   0.1868333   0.1170000   0.2350000 -108.31667 -198.20000  -12.20000 3.045000 1.00
3    2 0.30       0   0.3035000   0.2130000   0.3390000  -69.10000 -164.00000  -14.20000 2.235000 1.02
4    3 0.30       0   0.1917500   0.1010000   0.2320000  -85.70000 -151.60000   -3.80000 2.539167 0.54
5    2 0.33       0   0.3067500   0.2200000   0.3440000  -59.16667 -138.20000  -12.60000 2.077500 0.87
6    3 0.33       0   0.1936667   0.1090000   0.2320000  -68.56667 -144.80000   -3.40000 2.236667 0.31
     M
1 5.04
2 4.02
3 2.77
4 3.53
5 2.63
6 3.25

就是这样!