将列表(列)打印为CSV

时间:2019-03-04 03:00:12

标签: r

我有一个由R函数pcaObject生成的列表prcomp

pcaObject <- prcomp(x, center = TRUE, scale. = TRUE)

pcaObject是一个列表-请参见下面的dput。

当我尝试以下功能来打印输出时(尤其是PC列)

df <- data.frame(pcaObjec)
write.csv(df, "P:/OutputTS.csv", row.names = F, na="")

我得到了错误:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ‘"prcomp"’ to a data.frame

pcaObject的dput:

dput(pcaObject)
structure(list(sdev = c(3.96386401883026, 0.371262969758643, 
0.29805871259465, 0.156884095635089, 0.124049809102264, 0.0860271543065871, 
0.0702954411645217, 0.0653831227598336, 0.0487207900587564, 0.0352410840915365, 
0.0228097461869148, 0.0168155929257151, 0.00837290910119958, 
1.03888134938133e-16), rotation = structure(c(-0.251132620983772, 
-0.250036805617014, -0.250410265772156, -0.248729743637022, -0.251510783187722, 
-0.248758329513378, -0.247875704862118, -0.250899035552594, -0.249734658857896, 
-0.250216494797058, -0.250173189878289, -0.249886950787212, -0.250698557921361, 
-0.249129623224217, -0.249490830594251, -0.251285348565002, 0.1149385225544, 
-0.0706839687136611, 0.0302735147858248, 0.2385670529597, -0.0371553874718862, 
-0.415827467090189, -0.467736963101989, -0.125275922818765, 0.325515460587937, 
0.24778343214805, 0.250029931872436, 0.36564089741004, 0.0093752885552472, 
-0.288619727133923, -0.252032380093566, 0.0687205856200324, 0.226403137219826, 
0.371051553981361, 0.264023089906086, 0.444331601264287, 0.0297929506392822, 
0.136973788974268, 0.0821859604298977, 0.199822638593142, -0.193529777391699, 
-0.166385719117488, -0.172301178573551, 0.00398099725641139, 
-0.351214940341657, -0.315071229385823, -0.325250493609293, -0.233107953232043, 
0.0785023443251121, -0.191931120404559, -0.403283941139839, 0.09751613065039, 
0.392793370630643, 0.164659174118636, -0.292966132422363, 0.360138323635876, 
-0.0629336818210646, 0.0548343242409484, -0.429474110972193, 
0.0330196153384357, 0.136858038549663, 0.132402030021276, -0.317637239491194, 
0.24138114448077, -0.0224186434020077, -0.338702643902654, 0.423895558971745, 
0.0428887632790135, 0.0509658015897322, 0.223073639045513, -0.279056105752921, 
-0.0307133881985013, 0.102766495143579, -0.502443930176957, 0.205162944348455, 
0.0697585125459092, 0.0627028007944759, 0.410855112037809, -0.239561797468895, 
-0.178839845517656, -0.501176888081427, -0.0582434017354892, 
0.448782403175441, -0.0804927032164035, 0.29055970924396, -0.149336842349218, 
-0.013211557846777, 0.23260177246399, -0.365905288447767, 0.145104981651216, 
0.0993100056439826, -0.078723547543366, 0.314351647524268, -0.299840861385341, 
-0.0861940850438603, 0.0968876394410199, -0.201778938176567, 
-0.241528375656552, -0.0690894796472369, 0.614258023853814, -0.0944108318123788, 
0.0241943286643915, 0.0631335551181668, -0.320706867023405, -0.401261618595919, 
0.330121154498166, -0.0452401675434747, 0.0983140528867455, 0.00625394222803678, 
0.307973923433854, 0.0783396623122151, -0.140705990611096, 0.265029888426847, 
-0.587850444326808, 0.0498975420459495, 0.115566868104076, -0.410914339262958, 
0.0970421235987468, 0.0365763991770912, 0.472047393213039, -0.0771176604339353, 
0.0480473496427421, 0.122797519435341, -0.121910843342086, -0.0514795750606867, 
-0.264452906879168, 0.217022869519729, 0.0896081854350561, 0.0117913973219658, 
0.227554850339973, 0.153971527530776, -0.288938075677179, -0.346817298165995, 
0.4811592642693, -0.406425541548965, -0.0225779803373695, -0.145185748917912, 
0.487519697525297, 0.0576146704733379, -0.0898468842439095, -0.0816041475498578, 
0.104903097121474, -0.184378647233265, 0.0399211220564946, -0.287567522645956, 
0.388180220872585, -0.22260958622022, 0.298991677040834, -0.482130808189775, 
-0.0802123186049899, -0.172476162959844, 0.250554706136809, 0.00882763453332421, 
-0.322901364144029, 0.0955918033625254, -0.103698111731714, 0.367797410686567, 
0.0970395889096519, 0.00120374915425345, 0.165326313475823, 0.263511636164441, 
0.0394357090897246, 0.234900299088479, 0.168554340407161, 0.1134963225599, 
-0.173366237121736, -0.221281373545769, -0.293982229755118, -0.10428944616536, 
-0.0961293759903748, -0.0507482134318119, -0.538210959989751, 
-0.114338503544724, 0.0639785323381142, 0.138911901668782, 0.564147883649763, 
-0.305058762810582, -0.109758774515861, -0.0481150939830698, 
0.0158729796451733, -0.110859458841409, -0.0074831277379482, 
0.424615855178192, 0.0533749394388935, 0.176848977866661, 0.0659346086850714, 
0.208458381340704, -0.040231487337358, -0.391617702306986, 0.143995391761434, 
-0.504772652302533, 0.432713406786188, 0.344272969974062, -0.0677078394977572, 
-0.147272161688341, -0.00267654504914853, -0.0318965291451207, 
0.169613709928944, 0.282196735701957, -0.323784729264833, -0.104382606295455, 
-0.0127413567930727, 0.21243778647154, -0.191018899621071, 0.558596961291463, 
-0.220288195338771, -0.431952110465128, -0.0334900834940877, 
0.326723456269055, 0.081682830130099, 0.0204852304379668, -0.267504682403714, 
-0.135165047665943, -0.324995508502683, 0.0623958536324647, 0.0790701765760546, 
-0.579541768465202, -0.13478233898426, 0.072212131580383, 0.458388521931952, 
-0.0252236856689504, 0.242434713919513, -0.0895157851740492, 
0.209214776410063), .Dim = c(16L, 14L), .Dimnames = list(c("c1372", 
"c5244", "c5640", "c6164", "b1372", "b5244", "b5640", "b6164", 
"v1372", "v5244", "v5640", "v6164", "bv1372", "bv5244", "bv5640", 
"bv6164"), c("PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", 
"PC8", "PC9", "PC10", "PC11", "PC12", "PC13", "PC14"))), center = c(c1372 = 7.17301246971429, 
c5244 = 3.63328133471429, c5640 = 4.94371748871429, c6164 = 4.68130610992857, 
b1372 = 1.92884902642857, b5244 = 2.57642880128571, b5640 = 2.29536809957143, 
b6164 = 1.294378252, v1372 = 0.0240165342857143, v5244 = 0.0145241637142857, 
v5640 = 0.0169409162857143, v6164 = 0.0155530712142857, bv1372 = 0.00879025478571429, 
bv5244 = 0.0137292458571429, bv5640 = 0.0104053299285714, bv6164 = 0.00588060442857143
), scale = c(c1372 = 2.47829379779685, c5244 = 1.31960713001454, 
c5640 = 1.71865548873363, c6164 = 1.79050732007234, b1372 = 0.721518931822248, 
b5244 = 0.948104827306886, b5640 = 0.77948107651639, b6164 = 0.515183606999586, 
v1372 = 0.00857039217557237, v5244 = 0.00525876756282377, v5640 = 0.00603408021775135, 
v6164 = 0.00618415705738276, bv1372 = 0.00322727921084455, bv5244 = 0.00495633502015049, 
bv5640 = 0.00357910047130669, bv6164 = 0.00243752454497299), 
    x = structure(c(4.98027031009745, 3.85265405101666, 3.62790424313918, 
    3.56262840601047, 2.72473429379308, 1.43170503060754, 1.6475525780209, 
    0.976998712967587, 0.029542503781289, -2.3438417426662, -2.46866487072471, 
    -4.59817485178769, -5.53028173228237, -7.89302693197318, 
    0.353565776159628, 0.456009397487711, -0.165068334278154, 
    0.0946255971915289, 0.0776460945286551, 0.293375859280709, 
    -0.619634052271705, -0.310827947481586, -0.0956915776305262, 
    0.120263340442078, -0.741059202027703, -0.158606725449101, 
    0.275867265064582, 0.419534508983888, 0.0499351309961266, 
    -0.0850582520677813, -0.0141790846429926, 0.280869617824888, 
    0.106205422862449, -0.556381885285481, -0.302493946411801, 
    0.35921704951926, 0.249446361510719, -0.266071326627976, 
    -0.266224882097219, 0.35622057154512, 0.352483318899409, 
    -0.26396809602472, 0.0437246573181607, -0.0380179059547391, 
    0.0289139275953211, 0.160989601159793, -0.116851967568154, 
    0.0230091918251678, -0.0216996534440354, 0.187283832440309, 
    -0.203875248977234, -0.120873839856439, -0.0667690020439385, 
    0.191934492515901, -0.298793892438228, 0.231025807428117, 
    -0.104303980020078, 0.0167658668758121, -0.196345270062272, 
    -0.0750834763149325, 0.086046523262512, 0.0773420540169531, 
    -0.0142782792788702, 0.176182930738111, 0.102401959275087, 
    0.212327903009999, -0.117482249105866, 0.0596983913499743, 
    -0.144866926067715, -0.0784054476787126, -0.0765584959819813, 
    -0.00545292710135198, 0.0231919763401417, 0.0497610094035505, 
    0.0407646719582848, -0.0817825233716301, -0.010415162450025, 
    0.0688836099855234, 0.115303747999583, -0.0227592348759864, 
    -0.000772997797818496, -0.195669967372709, -0.0456256501304102, 
    0.141131943394829, -0.0222071931122594, 0.00575607533227661, 
    -0.116907775545307, -0.00508691577124393, 0.0942724364328044, 
    0.0798875922091006, 0.0700222222887531, -0.00300321275671783, 
    0.0134233554305223, -0.168253735219611, -0.00686377676897583, 
    0.0107818154473825, 0.027576120590772, 0.0206029914425022, 
    0.0210891824211711, 0.0429393615473976, 0.0649665502122391, 
    -0.156915294006484, 0.0793632973562472, -0.0098937556288551, 
    -0.0847402164515301, 0.0702304468474727, -0.0365747187129404, 
    -0.0435317050890943, 0.047119764678398, 0.0058504905708779, 
    -0.0148835014444554, 0.0149800976995561, 0.0398978829830871, 
    0.0494916951969784, 0.00035170112543785, -0.0662595079137798, 
    -0.0965887273071021, -0.0104040493286948, 0.039929938831651, 
    0.000890275559021495, 0.0912075601362972, -0.0359055270556502, 
    -0.0283890416161073, 0.0209184020319234, -0.0188339084459388, 
    0.0136933058028797, -0.0866005712546497, 0.0444956582651886, 
    0.0432278125095427, 0.0111177019672117, -0.0313291870545565, 
    0.0397593921813287, -0.00696241293986309, 0.0208621843639958, 
    -0.0088557677990337, -0.0175228734767378, -0.0164637531027947, 
    0.00264333681804141, 0.0245855663456727, -0.018957086823349, 
    0.0182842036659842, 0.0149931043223561, -0.0342481912148661, 
    0.00527379562146679, -0.0308507026646042, 0.00282641950955426, 
    -0.0050472480215811, 0.0393393448955918, -0.0222693828292194, 
    0.00106694606684365, 0.0246586880344768, -0.0282432293134118, 
    0.0223918281929606, -0.00817557626555088, -0.00289921943159299, 
    0.0134510137078096, 0.00187594550579073, -0.010629558509283, 
    0.00836201571153411, -0.0248707919286508, 0.0354112640746904, 
    0.00550937234730756, -0.0253094353367176, 0.0085526588067959, 
    -0.0230363528710801, -0.00300227946557374, 0.0100762035374821, 
    0.00650916385148836, 0.00526740498470918, -0.0193116243089706, 
    0.00793095786022061, -0.00522669407647568, -0.00263014998198399, 
    0.0108598278983311, 0.00345014299821945, 0.00911712455003744, 
    0.000395255801043757, -0.000474786787270289, -0.0108890084893557, 
    -0.00546886913553327, 0.00718934917604375, -0.000208930489015519, 
    5.55111512312578e-17, -2.77555756156289e-17, 2.22044604925031e-16, 
    -2.77555756156289e-17, 0, 4.16333634234434e-17, -6.93889390390723e-17, 
    -4.16333634234434e-17, -9.0205620750794e-17, -3.88578058618805e-16, 
    -2.22044604925031e-16, -3.60822483003176e-16, -3.88578058618805e-16, 
    -1.11022302462516e-16), .Dim = c(14L, 14L), .Dimnames = list(
        NULL, c("PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", 
        "PC8", "PC9", "PC10", "PC11", "PC12", "PC13", "PC14")))), class = "prcomp")

如何将PC列列表以CSV格式打印?

1 个答案:

答案 0 :(得分:0)

您没有提供可复制的源(PCA之前)样本数据,因此我们以USArrests作为样本数据。

执行PCA

pc <- prcomp(USArrests)

输出对象pc是类list的{​​{1}};您可以使用

检查"prcomp"元素
list

要从str(pc) #List of 5 # $ sdev : num [1:4] 83.73 14.21 6.49 2.48 # $ rotation: num [1:4, 1:4] 0.0417 0.9952 0.0463 0.0752 -0.0448 ... # ..- attr(*, "dimnames")=List of 2 # .. ..$ : chr [1:4] "Murder" "Assault" "UrbanPop" "Rape" # .. ..$ : chr [1:4] "PC1" "PC2" "PC3" "PC4" # $ center : Named num [1:4] 7.79 170.76 65.54 21.23 # ..- attr(*, "names")= chr [1:4] "Murder" "Assault" "UrbanPop" "Rape" # $ scale : logi FALSE # $ x : num [1:50, 1:4] 64.8 92.8 124.1 18.3 107.4 ... # ..- attr(*, "dimnames")=List of 2 # .. ..$ : chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ... # .. ..$ : chr [1:4] "PC1" "PC2" "PC3" "PC4" # - attr(*, "class")= chr "prcomp" 对象中提取变量载荷

pr

使用您的数据

df <- pc$rotation
df
#                PC1         PC2         PC3         PC4
#Murder   0.04170432 -0.04482166  0.07989066 -0.99492173
#Assault  0.99522128 -0.05876003 -0.06756974  0.03893830
#UrbanPop 0.04633575  0.97685748 -0.20054629 -0.05816914
#Rape     0.07515550  0.20071807  0.97408059  0.07232502

然后将df <- pcaObject$rotation # PC1 PC2 PC3 PC4 PC5 PC6 #c1372 -0.2511326 0.114938523 0.226403137 0.07850234 -0.02241864 -0.50117689 #c5244 -0.2500368 -0.070683969 0.371051554 -0.19193112 -0.33870264 -0.05824340 #c5640 -0.2504103 0.030273515 0.264023090 -0.40328394 0.42389556 0.44878240 #c6164 -0.2487297 0.238567053 0.444331601 0.09751613 0.04288876 -0.08049270 #b1372 -0.2515108 -0.037155387 0.029792951 0.39279337 0.05096580 0.29055971 #b5244 -0.2487583 -0.415827467 0.136973789 0.16465917 0.22307364 -0.14933684 #b5640 -0.2478757 -0.467736963 0.082185960 -0.29296613 -0.27905611 -0.01321156 #b6164 -0.2508990 -0.125275923 0.199822639 0.36013832 -0.03071339 0.23260177 #v1372 -0.2497347 0.325515461 -0.193529777 -0.06293368 0.10276650 -0.36590529 #v5244 -0.2502165 0.247783432 -0.166385719 0.05483432 -0.50244393 0.14510498 #v5640 -0.2501732 0.250029932 -0.172301179 -0.42947411 0.20516294 0.09931001 #v6164 -0.2498870 0.365640897 0.003980997 0.03301962 0.06975851 -0.07872355 #bv1372 -0.2506986 0.009375289 -0.351214940 0.13685804 0.06270280 0.31435165 #bv5244 -0.2491296 -0.288619727 -0.315071229 0.13240203 0.41085511 -0.29984086 #bv5640 -0.2494908 -0.252032380 -0.325250494 -0.31763724 -0.23956180 -0.08619409 #bv6164 -0.2512853 0.068720586 -0.233107953 0.24138114 -0.17883985 0.09688764 # PC7 PC8 PC9 PC10 PC11 #c1372 -0.201778938 0.26502989 0.01179140 -0.287567523 0.26351164 #c5244 -0.241528376 -0.58785044 0.22755485 0.388180221 0.03943571 #c5640 -0.069089480 0.04989754 0.15397153 -0.222609586 0.23490030 #c6164 0.614258024 0.11556687 -0.28893808 0.298991677 0.16855434 #b1372 -0.094410832 -0.41091434 -0.34681730 -0.482130808 0.11349632 #b5244 0.024194329 0.09704212 0.48115926 -0.080212319 -0.17336624 #b5640 0.063133555 0.03657640 -0.40642554 -0.172476163 -0.22128137 #b6164 -0.320706867 0.47204739 -0.02257798 0.250554706 -0.29398223 #v1372 -0.401261619 -0.07711766 -0.14518575 0.008827635 -0.10428945 #v5244 0.330121154 0.04804735 0.48751970 -0.322901364 -0.09612938 #v5640 -0.045240168 0.12279752 0.05761467 0.095591803 -0.05074821 #v6164 0.098314053 -0.12191084 -0.08984688 -0.103698112 -0.53821096 #bv1372 0.006253942 -0.05147958 -0.08160415 0.367797411 -0.11433850 #bv5244 0.307973923 -0.26445291 0.10490310 0.097039589 0.06397853 #bv5640 0.078339662 0.21702287 -0.18437865 0.001203749 0.13891190 #bv6164 -0.140705991 0.08960819 0.03992112 0.165326313 0.56414788 # PC12 PC13 PC14 #c1372 -0.305058763 0.344272970 0.32672346 #c5244 -0.109758775 -0.067707839 0.08168283 #c5640 -0.048115094 -0.147272162 0.02048523 #c6164 0.015872980 -0.002676545 -0.26750468 #b1372 -0.110859459 -0.031896529 -0.13516505 #b5244 -0.007483128 0.169613710 -0.32499551 #b5640 0.424615855 0.282196736 0.06239585 #b6164 0.053374939 -0.323784729 0.07907018 #v1372 0.176848978 -0.104382606 -0.57954177 #v5244 0.065934609 -0.012741357 -0.13478234 #v5640 0.208458381 0.212437786 0.07221213 #v6164 -0.040231487 -0.191018900 0.45838852 #bv1372 -0.391617702 0.558596961 -0.02522369 #bv5244 0.143995392 -0.220288195 0.24243471 #bv5640 -0.504772652 -0.431952110 -0.08951579 #bv6164 0.432713407 -0.033490083 0.20921478 写到CSV。