NA values not being excluded in `cor`
Asked Answered
rna
B

2

41

To simplify, I have a data set which is as follows:

b <- 1:6
# > b
# [1] 1 2 3 4 5 6
jnk <- c(2, 4, 5, NA, 7, 9)
# > jnk
# [1]  2  4  5 NA  7  9

When I try:

cor(b, jnk, na.rm=TRUE)

I get:

> cor(b, jnk, na.rm=T)
  Error in cor(b, jnk, na.rm = T) : unused argument (na.rm = T)

I've also tried na.action = na.exclude, etc. None seem to work. It'd be really helpful to know what the issue is and how I can fix it. Thanks.

Belenbelesprit answered 14/7, 2015 at 16:38 Comment(0)
G
90

TL; DR: Use instead:

cor(b, jnk, use="complete.obs")

Read ?cor:

cor(x, y = NULL, use = "everything",
     method = c("pearson", "kendall", "spearman"))

It doesn't have na.rm, it has use.

an optional character string giving a method for computing covariances in the presence of missing values. This must be (an abbreviation of) one of the strings "everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs".

Pick one. Details of what each does is in the Details section of ?cor.

Granicus answered 14/7, 2015 at 16:42 Comment(4)
A very annoying part about R is the inconsistency w.r.t. na.rm and its variants...Rodman
at least help(foo) is consistent.Granicus
Yes. Still I hope they (in all base/out of the box functions, at least) unify at some point...Rodman
Brilliant thank you! For anyone else looking for the qiock solution, to exclude NA's from the cor() function, use the argument: use="complete.obs" use="complete.obs"Belenbelesprit
P
11

Just to make sure the answer to this question is clear.

To ignore NA, use

b <- 1:6
jnk <- c(2, 4, 5, NA, 7, 9)
cor(b, jnk, use="complete.obs")
[1] 0.9905977
Prosthodontics answered 31/10, 2017 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.