-2

I am also dealing with the same problem in principal component analysis in R.The same error message is being shown:

Error in cov.wt(z) : 'x' must contain finite values only

I have checked your answers for this, but still its not working. The user2662565 had the same problem.

This is the program code i have used:

***data<-read.csv(file.choose(),header=T)
data      
#Calculate number of rows and col
rows<-length(data[,1])
rows
cols<-length(data[1,])
cols
#Remove header and save each column to a matrix
for ( i in 1:rows){
   for ( j in 1:cols){
       if(data[i,j]=="NA"){
         data[i,j]="0"
       }
   }
}
pca_a<-princomp(data, cor=True, covmat=NULL, scores=TRUE)
#Print scree plot
require(graphics)
plot(pca_a)
#plot pca
biplot(pca_a)***
currarpickt
  • 2,290
  • 4
  • 24
  • 39
  • Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Jean Mar 08 '17 at 04:15
  • 2
    We have no idea what your data looks like. Run `str(data)` and copy the output. Click 'edit' on your question and paste the result. – Pierre L Mar 08 '17 at 04:16
  • 1
    Do your future self a favor and stop using `file.choose()`. Will you know for sure which file to choose a week, month or a year from now? – Roman Luštrik Mar 08 '17 at 22:05

1 Answers1

3

First, you can replace rows<-length(data[,1]) with rows <- nrow(data) and cols<-length(data[1,]) with cols <- ncol(data).

Second, you should provide a reproducible example by making a toy dataset. I cannot use the same data as you because data<-read.csv(file.choose(),header=T) will attempt to choose a file on my computer and your data file is not on my computer. Can you make a toy dataset like data <- matrix(rnorm(100), nrow = 10, ncol = 10) and check if your code works?

Third, princomp doesn't work if any of the values of the data are non-numeric (e.g. characters). In your code, you are assigning data[i, j] <- '0', which means that you are making that value of data a string of the digit 0, not the actual number 0. You can prevent your error by making sure that all of the values of data are numeric values.

Michael Frasco
  • 404
  • 3
  • 7