prcomp,colMeans中的错误(x,na.rm = TRUE):当我的数据是数字时,'x'必须是数字?

时间:2017-08-19 03:40:13

标签: r pca

我正在尝试使用prcomp

对一组数据进行PCA
SS.chem<-read.csv("PCASS3.csv")
SS.pca <- prcomp(SS.chem,
             center = TRUE,
             scale. = TRUE)

返回以下错误

> SS.pca <- prcomp(SS.chem,
+                  center = TRUE,
+                  scale. = TRUE)
Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric

然而,当我测试以确保我的数据是数字时,他们......是吗?

sapply(SS.chem, class)
Capture.zone       X_.18O.NO3       X_.15N.NO3            NO3.T 
   "numeric"        "numeric"        "numeric"        "numeric" 
   WellDepth          PopDens              Pop       DepthToUfa 
   "numeric"        "numeric"        "numeric"        "numeric" 
        SOM.            Clay.     ConfineThick            LUDom 
   "numeric"        "numeric"        "numeric"        "numeric" 
  ostdscount              Ag.          Barren.          Forest. 
   "numeric"        "numeric"        "numeric"        "numeric" 
Transportation. UplandNonforest.           Urban.         Wetland. 
   "numeric"        "numeric"        "numeric"        "numeric" 
GolfCourses. ImprovedPasture.      FieldCrops.          Citrus. 
   "numeric"        "numeric"        "numeric"        "numeric" 
 Ornamental.       HorseFarm.          Sewage. 
   "numeric"        "numeric"        "numeric" 

有什么我想念的吗?为什么它仍然给我这个错误?

编辑:以下是我的数据行

> str(SS.chem)
List of 27
$ Capture.zone    : num [1:48] 1000 1000 1000 1000 1000 100 2 1000 2 2 ...
$ X_.18O.NO3      : num [1:48] 9.23 7.74 10.75 5.37 0 ...
$ X_.15N.NO3      : num [1:48] 10.67 6.78 9.53 7.88 3.03 ...
$ NO3.T           : num [1:48] 0.49 0 0.01 0.38 0.04 0 0.02 1.73 0.25 ...
$ WellDepth       : num [1:48] 0 190 132 0 0 0 0 0 0 0 ...
$ PopDens         : num [1:48] 246.092 1.102 21.331 246.092 0.359 ...
$ Pop             : num [1:48] 313.417 1.404 27.166 313.417 0.457 ...
$ DepthToUfa      : num [1:48] 121.9 107.9 79.1 121.9 36.4 ...
$ SOM.            : num [1:48] 1.12 1.23 1.23 1.12 60 ...
$ Clay.           : num [1:48] 3.5 3.5 3.5 3.5 3 ...
$ ConfineThick    : num [1:48] 85.1 91.4 61.7 85.1 0 ...

1 个答案:

答案 0 :(得分:0)

您可以将列表转换为数据框。

# Example data:

ls <- list(Capture.zone=c(1000, 1000 ,1000, 1000, 1000),
X_.18O.NO3 = c( 9.23, 7.74, 10.75, 5.37, 0),
X_.15N.NO3 = c(10.67, 6.78, 9.53, 7.88, 3.03),
NO3.T  = c(0.49, 0, 0.01, 0.38, 0.04),
WellDepth = c( 0, 190, 132, 0, 0))

#convert to a data frame
df <- data.frame(ls)

#get the pricipal components
prcomp(df)

#               PC1         PC2         PC3          PC4         PC5
#Capture.zone  0.000000000  0.00000000  0.00000000  0.000000000   1
#X_.18O.NO3    0.023172366  0.77806115 -0.58173783  0.235934281   0
#X_.15N.NO3    0.003266792  0.62719370  0.70307503 -0.335116238   0
#NO3.T        -0.001764784  0.02911366  0.40880900  0.912153762   0
#WellDepth     0.999724590 -0.02003257  0.01190818 -0.002763407   0