R如何将数据帧列矩阵扩展为数据帧列

时间:2014-07-13 13:21:05

标签: r

我有以下数据帧df,它是由TERR在一个较大的数据帧上进行聚合调用生成的,该数据帧在列上执行了两个函数。因此,在这种情况下,在PC1和PC2上计算平均值和标准误差。问题是我现在需要单独调用这些值。如果我试试

df$PC1.mn它返回NULL然而df$PC1我得到两者的列表。我需要这些矩阵是单独的列。有没有办法实现这个目标?

编辑:除第一列外,每列是2列的矩阵,需要扩展为单独的列。

   TERR     PC1.mn    PC1.sem     PC2.mn    PC2.sem      
1 MENS_10 0.82030703 0.05964201 0.60991007 0.03922947 
2 MENS_14 0.22284209 0.07027761 0.01079179 0.03857742 

str(df$PC1)

的输出
num [1:19, 1:2] 0.82 0.223 1.094 1.04 0.812 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "mn" "sem"

dput(df)

的输出
structure(list(TERR = structure(1:19, .Label = c("MENS_10", "MENS_14", 
"MENS_16", "MENS_18", "MENS_2 ", "MENS_20", "MENS_24", "MENS_25", 
"MENS_26", "MENS_29", "MENS_31", "MENS_32", "MENS_40", "MENS_42", 
"MENS_44", "MENS_46", "MENS_5", "MENS_8", "MENS_E"), class = "factor"), 
    PC1 = structure(c(0.820307033727786, 0.222842088186464, 1.09398346334857, 
    1.03987566591133, 0.811508841644094, 1.24651570248768, 1.05238710884733, 
    0.843375523572735, 0.685570397342129, 1.09996139295587, 1.1151090622921, 
    -0.298832473495758, 1.29946851248417, 0.946576117066279, 
    0.187808783643163, 0.893302196795568, 0.983720713568758, 
    0.886156614045068, 1.25822951068011, 0.0596420103795457, 
    0.0702776122855427, 0.0404015364189346, 0.0447926762952018, 
    0.036140124264161, 0.0160560001285343, 0.0231077996353513, 
    0.0454453863107384, 0.0732883733170798, 0.0419403787520002, 
    0.0529142110353097, 0.113250160103334, 0.0317033323860567, 
    0.0499866649881185, 0.131566191776678, 0.0726248148670481, 
    0.0468352242077059, 0.0252178951026526, 0.0201191567959382
    ), .Dim = c(19L, 2L), .Dimnames = list(NULL, c("mn", "sem"
    ))), PC2 = structure(c(0.609910070963486, 0.010791790442562, 
    -0.019036172787209, 0.414690046178149, 0.386429802848653, 
    0.743955055344087, 0.906095700713572, 0.881751910465456, 
    0.393712721920078, 0.945199289980582, 0.778368954953892, 
    0.563653938290256, -0.225664575518917, 0.752018591494644, 
    0.0721395199667787, -0.948596863396142, 0.943337613028796, 
    -0.107366982269548, -0.65976401574119, 0.039229472726257, 
    0.0385774221025136, 0.4269694476559, 0.0679840394899709, 
    0.041880478016631, 0.0143178196405501, 0.00951587961886708, 
    0.0196930171913273, 0.0984619155288124, 0.0079466733534216, 
    0.055388457985743, 0.0349441555633137, 0.214629105591149, 
    0.0636257142218683, 0.0888801495049588, 0.183337779507489, 
    0.010175333454896, 0.0628768381904623, 0.0795386419888819
    ), .Dim = c(19L, 2L), .Dimnames = list(NULL, c("mn", "sem"
    ))), PC3 = structure(c(-0.326302104622856, -0.257388824025022, 
    -0.717982657985263, -0.657589966361892, -0.321736105982529, 
    0.410407200147062, -0.78002620224271, -0.692036731761886, 
    -0.0760579971344166, -0.593241418680366, 0.360266620210402, 
    -0.573274417731803, -0.197552757548519, 0.588238516866691, 
    -0.701976918873246, -0.0971384417779599, -0.360250102628055, 
    -0.0799437535436827, 0.400789302478491, 0.070699100114383, 
    0.0169569534700048, 0.00318869179967574, 0.0560397912451556, 
    0.0714868684576213, 0.0184154105360385, 0.0231515714580647, 
    0.0446593876491272, 0.0728436000061978, 0.0617518825334356, 
    0.100569655433215, 0.0559660127669167, 0.049985590357157, 
    0.102855069056182, 0.0655873698867967, 0.0507424531437847, 
    0.0706588044346223, 0.029594746707871, 0.0259242551078958
    ), .Dim = c(19L, 2L), .Dimnames = list(NULL, c("mn", "sem"
    ))), PC1_REF = structure(c(-1.79014822762748, -2.19654556764402, 
    -0.706670680491541, 0.84222997068149, -0.200035213817825, 
    0.76159457082468, -2.96784934132109, -2.36363634780223, -1.6779292791128, 
    -1.28863092729594, -1.71270440574731, -0.749560834674291, 
    1.22805142927275, -2.49193710281539, -3.51171040879644, -0.0312246304760417, 
    -2.42278943771601, -2.58615357515189, 1.09617164069407, 0.121525527234789, 
    0.0627107120532972, 0.65184784602933, 0.0163433239917639, 
    0.0858171773639523, 0.0218127160037072, 0.198432324587447, 
    0.087394617281051, 0.0343381088419686, 0.151994945838798, 
    0.115252644765557, 0.0347868846563838, 0.0923561659189044, 
    0.209773659527996, 0.125483611165188, 0.164512129616854, 
    0.238097033334626, 0.115420144119389, 0.017684863965389), .Dim = c(19L, 
    2L), .Dimnames = list(NULL, c("mn", "sem"))), PC2_REF = structure(c(0.0191314514849518, 
    0.0379648844721645, -0.454797258156674, -0.316881255342318, 
    -0.222640182135541, 0.398810588895599, 0.065801297918062, 
    0.163708550103078, 0.360407839286103, 0.487240920556612, 
    0.0595015980154598, 0.657245803004009, -0.0516881124702257, 
    -1.40480795448723, 0.352986631679451, 0.36070244532732, -0.359172025543314, 
    -1.22953037087422, -2.15254915446959, 0.0492863335469464, 
    0.024079577386548, 0.310968952096746, 0.0976750590235698, 
    0.0558342658138735, 0.0262868860364361, 0.0255728783826103, 
    0.0275967588543879, 0.0348972306236099, 0.0584917554768032, 
    0.0612924232752413, 0.019086945976421, 0.193539766002865, 
    0.189392084648141, 0.0311901287757335, 0.0446153794854082, 
    0.0566124654632608, 0.114494991588691, 0.109907100018088), .Dim = c(19L, 
    2L), .Dimnames = list(NULL, c("mn", "sem"))), PC3_REF = structure(c(-0.785662558526182, 
    -0.477272546063659, -1.00517995097847, -1.02030113940379, 
    1.24398872718819, 0.607291320538, -1.46190383080967, -1.28148551680627, 
    -0.507632811627629, -0.347706128537224, 0.0289620787098363, 
    -0.052999165429303, -0.029647814698048, 1.17566638493985, 
    -0.341007156514732, -0.80350511213881, -0.448713106410453, 
    1.9067734261776, 0.355448489072199, 0.0680129554696798, 0.0452539888036221, 
    0.107604471353131, 0.0205028642828262, 0.0544948584039173, 
    0.0157416677586763, 0.0880077236071284, 0.0647519131064312, 
    0.112523230074974, 0.116200699969251, 0.131793809035836, 
    0.0375505290549086, 0.124128738801318, 0.147075456084009, 
    0.106671365313098, 0.0597926204825535, 0.10691526418452, 
    0.0582383571872046, 0.0223774590546338), .Dim = c(19L, 2L
    ), .Dimnames = list(NULL, c("mn", "sem")))), .Names = c("TERR", 
"PC1", "PC2", "PC3", "PC1_REF", "PC2_REF", "PC3_REF"), row.names = c(NA, 
-19L), class = "data.frame")

1 个答案:

答案 0 :(得分:3)

df是一个包含7列的data.frame,除了第一列外,每列都是2列的矩阵:

> str(df)
'data.frame':   19 obs. of  7 variables:
 $ TERR   : Factor w/ 19 levels "MENS_10","MENS_14",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ PC1    : num [1:19, 1:2] 0.82 0.223 1.094 1.04 0.812 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "mn" "sem"
 $ PC2    : num [1:19, 1:2] 0.6099 0.0108 -0.019 0.4147 0.3864 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "mn" "sem"
 $ PC3    : num [1:19, 1:2] -0.326 -0.257 -0.718 -0.658 -0.322 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "mn" "sem"
 $ PC1_REF: num [1:19, 1:2] -1.79 -2.197 -0.707 0.842 -0.2 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "mn" "sem"
 $ PC2_REF: num [1:19, 1:2] 0.0191 0.038 -0.4548 -0.3169 -0.2226 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "mn" "sem"
 $ PC3_REF: num [1:19, 1:2] -0.786 -0.477 -1.005 -1.02 1.244 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "mn" "sem"

因此df$PC1是第一个矩阵,df$PC1[, 1]dfPC1[, "mn"]PC1矩阵的第一列,df$PC1[, 2]df$PC1[, "sem"]PC1矩阵的第二列等

这会将df转换为13列的平面数据框:

do.call(data.frame, df)