Omit NA and data imputation before doing PCA analysis using R
For na.action
to have an effect, you need to explicitly supply a formula
argument:
princomp(formula = ~., data = mydf, cor = TRUE, na.action=na.exclude)
# Call:
# princomp(formula = ~., data = mydf, na.action = na.exclude, cor = TRUE)
#
# Standard deviations:
# Comp.1 Comp.2 Comp.3
# 1.3748310 0.8887105 0.5657149
The formula is needed because it triggers dispatch of princomp.formula
, the only princomp
method that does anything useful with na.action
.
methods('princomp')
[1] princomp.default* princomp.formula*
names(formals(stats:::princomp.formula))
[1] "formula" "data" "subset" "na.action" "..."
names(formals(stats:::princomp.default))
[1] "x" "cor" "scores" "covmat" "subset" "..."
How to get rid of NA's without erasing the values named NA
If your problem is with reading actual strings "NA" as NA values, read.csv2 function has an argument na.strings
, which has a default value of "NA"
. That should be changed to something different, maybe even ""
. I've also seen "<NA>"
used in some cases.
Related Topics
Extract Survival Probabilities in Survfit by Groups
Match.Call with Default Arguments
R: How to Select Files in Directory Which Satisfy Conditions Both on the Beginning and End of Name
Import Multiple Text Files in R and Assign Them Names from a Predetermined List
How to Retrieve the Most Repeated Value in a Column Present in a Data Frame
Clear Memory Allocated by R Session (Gc() Doesnt Help !)
Keyboard Shortcut for Inserting Roxygen #' Comment Start
Create Multiple Data Frames from One Based Off Values with a for Loop
How to Replace Numeric Codes with Value Labels from a Lookup Table
Get Name of X When Defining '(<-' Operator
Shiny: Switching Between Reactive Data Sets with Rhandsontable
How to Manipulate Null Elements in a Nested List
Fill Area Between Two Lines, with High/Low and Dates
References Truncated in Beamer Presentation Prepared in Knitr/Rmarkdown
Getting Both Column Counts and Proportions in the Same Table in R
Grouping with Custom Geom Fails - How to Inspect Internal Object from Draw_Panel()