R function prcomp fails with NA's values even though NA's are allowed

后端 未结 2 991
夕颜
夕颜 2020-12-15 04:28

I am using the function prcomp to calculate the first two principal components. However, my data has some NA values and therefore the function throws an error.

相关标签:
2条回答
  • 2020-12-15 04:45

    Yeah, it looks like a "feature" (bug) that na.action is completely ignored unless you use the formula interface. This has been brought up before on the R Development list.

    I think that this should be documented or flagged as a bug.

    Just to be clear, this would work because it accesses the formula interface:

    prcomp(~V1+V2, data=d, center = TRUE, scale = TRUE, na.action = na.omit)
    
    0 讨论(0)
  • 2020-12-15 05:04

    Another solution if you're not willing to use formula interface is

    prcomp(na.omit(d), center = TRUE, scale = TRUE)
    

    which consist of applying na.omit directly to the data frame.

    0 讨论(0)
提交回复
热议问题