user6652926
user6652926

Reputation: 49

How to apply PCA in R with using data with a factor in its first column?

My data is composed of 16 columns. The first one is a factor. The rest of my columns are a set of numerical numbers. When i have applied the PCA method in R , I found that PCA can not be applied on my data.frame because this latter contains factors in the first column.

library(FactoMineR)
data(MyData)
res.pca = PCA(MyData[,1:10], scale.unit=TRUE, ncp=5, graph=T)

It gives me this error:

Error in colMeans(X, na.rm = TRUE) : 'x' must be numeric
And : Warning message:
In PCA(data, scale.unit = TRUE, ncp = 5, graph = T) :
Missing values are imputed by the mean of the variable: you should use the 
imputePCA function of the missMDA package

When I changed the first colums which was recently declared as a factor with a set of numerical numbers, that works and it gives me good results. I can plot all correlation axes only with number in my axes while I need factors not numbers.
I would be very grateful if you could help me please?

Upvotes: 0

Views: 1708

Answers (1)

R.B
R.B

Reputation: 517

You should mention that your first column is a factor . So try to do this :

library(FactoMineR)
library(missMDA)
data(MyData)
## Imputation
res <- imputePCA(MyData[,2:10],ncp=5)
mydata=cbind(myData[,1],res$completeObs)
res.pca = PCA(mydata,quali.sup=1, scale.unit=TRUE, ncp=5, graph=T)

Upvotes: 1

Related Questions