NA values not being excluded in `cor`

穿精又带淫゛_ 提交于 2019-12-03 23:58:08
Spacedman

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.

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!