Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

r

I have looked for such a long time, and haven't been able to figure out how to run Principal Component Analysis in R with the csv file I have. I continue to get this error:

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

all I have so far is

data <- read.csv("2014 07 24 Pct Chg Variables.csv")
pca <- princomp(data3, cor=T)

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

I have some "" in my csv file, and have tried

data2 <- apply(data, 1, f1)
data3 <- as.numeric(data2)

where f1 is a function to apply the mean where the value is a blank.

like image 507
user2662565 Avatar asked Jul 24 '14 17:07

user2662565


1 Answers

princomp.default cannot deal with NA values:

USArrests[3,2] <- NA

princomp(USArrests, cor = TRUE)
#Error in cov.wt(z) : 'x' must contain finite values only

You need to handle NAs:

princomp(na.omit(USArrests), cor = TRUE)
#works

Or use princomp.formula:

princomp(~ ., data = USArrests, cor = TRUE)
#works too (by calling na.omit` per default)
like image 188
Roland Avatar answered Sep 18 '22 10:09

Roland