【问题标题】:prcomp, Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric, when my data is numeric?prcomp,colMeans(x,na.rm = TRUE)中的错误:'x'必须是数字,当我的数据是数字时?
【发布时间】:2018-01-27 18:36:34
【问题描述】:

我正在尝试使用 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 ...

【问题讨论】:

  • 你能显示几行数据吗?
  • 发布了一些数据的更新
  • 数据结构显示为列表,先转成数据框。

标签: r pca


【解决方案1】:

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

# 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

【讨论】:

    猜你喜欢
    • 2019-10-04
    • 2023-03-28
    • 2020-12-19
    • 2018-05-23
    • 2017-10-25
    • 2020-06-18
    • 2018-01-17
    • 2021-02-14
    • 1970-01-01
    相关资源
    最近更新 更多