Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NA values not being excluded in `cor`

Tags:

r

na

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.

like image 670
Charlie Avatar asked Jul 14 '15 16:07

Charlie


People also ask

How do I exclude missing values in R?

Firstly, we use brackets with complete. cases() function to exclude missing values in R. Secondly, we omit missing values with na. omit() function.

How do I remove missing values from a vector in R?

We can remove those NA values from the vector by using is.na(). is.na() is used to get the na values based on the vector index. !

What does a correlation matrix show?

A correlation matrix is simply a table which displays the correlation coefficients for different variables. The matrix depicts the correlation between all the possible pairs of values in a table. It is a powerful tool to summarize a large dataset and to identify and visualize patterns in the given data.

How do you calculate correlation in R?

Use the function cor. test(x,y) to analyze the correlation coefficient between two variables and to get significance level of the correlation.


1 Answers

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.

like image 191
Spacedman Avatar answered Sep 18 '22 06:09

Spacedman