R- infinite or missing values in 'x' for prcomp()

1.1k views Asked by At
Error in svd(x, nu = 0, nv = k) : infinite or missing values in 'x'

I am using prcomp() and got this error message. I tried to do a reprex but could not reproduce the error. I tried:

df[is.na(df)] <- 0##NA values to 0
df <-df[which(rowSums(df) > 0),]##rm rows with only zeros
df <- df[, colSums(df != 0) > 0]##rm cols with only zeros

df is a data.frame, that is a list and not numeric. I have run out of ideas! What else could I try?

1

There are 1 answers

1
Oliver On BEST ANSWER

While most of it looks right, one thing that might help out here, is to change how you check for zero-columns. In this matter it is better to check length(unique(x)) or possibly better var(x) < 10^-13 (X almost doesn't vary, so it has almost only 1 value).

df[is.na(df)] <- 0
infs <- apply(df, 2, is.infinite)
rowswithinf <- rowsums(infs) > 0
#df[infs]  #handle infinite rows
#df[rowswithinf, ]  #Handle infinite rows
df <- df[!rowswithinf, ]

# Handle columns that almost only have 1 value:
nzv <- sapply(df, var) < 10^-13
df <- df[, !nzv]
prcomp(df)