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

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)

Leave a Comment