如何识别一个列作为一个名称列的PCA与excel文件?



我是R的新手,我想做一个PCA。

这是我的数据集:

structure(list(Varietes.rep = c("EPS45_2", "EPS47_1", "EPS45_1", "OPM_2", "EPS41_2", "EPS46_1", "Belet_1", "EPS42_2", "BDA_2", "Osoro_1"), ASI = c(4, 6, 2, 0, 3, 5, 3, 2, 6, 2), DTSnum = c(19, 16, 18, 16, 20, 25, 22, 16, 23, 22), DTAnum = c(15, 10, 16, 16, 17, 20, 19, 14, 17, 20), Amplitude = c(9, 14, 9, 4, 7, 8, 11, 6, 10, 6), AUC = c(416.6667, 512.1622, 353.4483, 20.98765, 327.907, 288.6364, 400, 108.8608, 601.6393, 260.6557), Pourcentage.pieds.steriles.femelle = c(4, 2.27272727272727, 1.5625, 0, 5.8252427184466,7.69230769230769, 1.20481927710843, 1.0752688172043, 1.2987012987013, 2.66666666666667), Hauteur.moyenne = c(270.3676, 256.6753, 281.0508, 288.0988, 272.4674, 293.8163, 303.4189, 264.7375, 258.371, 294.4444), Hauteur.moyenne.soies =c(101.8154, 100.6533, 101.8947, 100.2716, 101.8372, 130.7391, 126.2361, 97.1125, 102.7705, 122.7097), Pourcentage.double.epis = c(29.4117647058824, 33.3333333333333, 41.1764705882353, 74.5098039215686, 15.6862745098039, 29.4117647058824, 47.0588235294118, 58.8235294117647, 27.4509803921569, 31.3725490196078)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))

我的列名是" variety .rep"对于我的PCA,我想精确地指出这个变量是我的个体。

我该怎么办?

base R中,我们可以在删除第一列的同时将row.names设置为第一列,并应用prcomp

prcomp(`row.names<-`(as.data.frame(df1[-1]), df1[[1]]))

与产出

Standard deviations (1, .., p=9):
[1] 1.740407e+02 1.940922e+01 1.244057e+01 4.747485e+00 2.481467e+00 1.197997e+00 3.621528e-01 1.236234e-01 4.044832e-15
Rotation (n x k) = (9 x 9):
PC1          PC2          PC3         PC4          PC5          PC6         PC7         PC8           PC9
ASI                                -0.009441536 -0.004237640  0.023921657 -0.19086237 -0.052048523 -0.315610214  0.20076598  0.69780588 -5.773503e-01
DTSnum                             -0.006867303  0.125533815  0.062339752 -0.13742720  0.558004323 -0.308139050 -0.23453973  0.40836369  5.773503e-01
DTAnum                              0.002574233  0.129771455  0.038418095  0.05343516  0.610052846  0.007471164 -0.43530572 -0.28944219 -5.773503e-01
Amplitude                          -0.013888223 -0.004206378 -0.058284888 -0.08600605 -0.513714294 -0.075437642 -0.83548049  0.14619983  2.371367e-15
AUC                                -0.996727987  0.013670006 -0.075289266  0.01591253  0.008442632 -0.005448888  0.01329253 -0.01212881  0.000000e+00
Pourcentage.pieds.steriles.femelle -0.001913776  0.047541419  0.149077548 -0.08786674 -0.152057897 -0.855253847  0.08990259 -0.45291086 -1.486224e-15
Hauteur.moyenne                     0.037010923  0.739041840 -0.244336259  0.59755584 -0.107680234 -0.097974355  0.04572905  0.11130594  1.144917e-16
Hauteur.moyenne.soies              -0.002450546  0.624853368  0.002744968 -0.73347830 -0.078457457  0.203460007  0.07873022 -0.13343280 -3.131176e-16
Pourcentage.double.epis             0.069412039 -0.168206275 -0.950298953 -0.17718858  0.094402269 -0.131187195  0.02483250 -0.07540691  3.469447e-17

指定的列需要是行名,因此您需要首先将列转换为行名:

library(magrittr)
pca = dat %>%
tibble::column_to_rownames("Varietes.rep") %>%
prcomp()
Standard deviations (1, .., p=9):
[1] 1.740407e+02 1.940922e+01 1.244057e+01 4.747485e+00 2.481467e+00 1.197997e+00 3.621528e-01 1.236234e-01 9.236234e-16
Rotation (n x k) = (9 x 9):
PC1          PC2          PC3         PC4          PC5          PC6         PC7         PC8           PC9
ASI                                -0.009441536 -0.004237640  0.023921657 -0.19086237 -0.052048523 -0.315610214  0.20076598  0.69780588  5.773503e-01
DTSnum                             -0.006867303  0.125533815  0.062339752 -0.13742720  0.558004323 -0.308139050 -0.23453973  0.40836369 -5.773503e-01
DTAnum                              0.002574233  0.129771455  0.038418095  0.05343516  0.610052846  0.007471164 -0.43530572 -0.28944219  5.773503e-01
Amplitude                          -0.013888223 -0.004206378 -0.058284888 -0.08600605 -0.513714294 -0.075437642 -0.83548049  0.14619983 -1.541798e-15
AUC                                -0.996727987  0.013670006 -0.075289266  0.01591253  0.008442632 -0.005448888  0.01329253 -0.01212881  1.000562e-16
Pourcentage.pieds.steriles.femelle -0.001913776  0.047541419  0.149077548 -0.08786674 -0.152057897 -0.855253847  0.08990259 -0.45291086  8.907345e-16
Hauteur.moyenne                     0.037010923  0.739041840 -0.244336259  0.59755584 -0.107680234 -0.097974355  0.04572905  0.11130594 -4.653036e-16
Hauteur.moyenne.soies              -0.002450546  0.624853368  0.002744968 -0.73347830 -0.078457457  0.203460007  0.07873022 -0.13343280  7.895899e-16
Pourcentage.double.epis             0.069412039 -0.168206275 -0.950298953 -0.17718858  0.094402269 -0.131187195  0.02483250 -0.07540691  1.981639e-16

最新更新