Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R run correlation, ignoring non-numeric data

Tags:

r

correlation

I am trying to run a correlation between all numberic values (the dataset contains columns of both numeric and non-numeric values) using the following code:

mydata= read.csv("C:\\full_path\\playerData.csv", header = TRUE)
mydata=data.frame(mydata)
vals=cor(mydata, use="complete.obs", method="pearson") 
write.csv(vals,"C:\\Users\\weiler\\Desktop\\RStudioOutput.csv")

based on this site: http://www.statmethods.net/stats/correlations.html I am getting the error:

Error in cor(mydata, use = "complete.obs", method = "pearson") : 'x' must be numeric

My error seems to be because some of the data is non-numeric. Is there a simple way to ignore the non-numeric data?

like image 862
Rilcon42 Avatar asked Jan 08 '15 22:01

Rilcon42


1 Answers

As David Arenburg said, you can use is.numeric and then subset. Here's an explanation.

mydata <-
  data.frame(alpha1=letters[1:10], alpha2=letters[11:20], 
             num1=runif(10), num2=runif(10))

#    alpha1 alpha2       num1       num2
# 1       a      k 0.02123999 0.50840184
# 2       b      l 0.23963061 0.27622386
# 3       c      m 0.32220265 0.69144157
# 4       d      n 0.08147787 0.59194675
# 5       e      o 0.15875212 0.61067014
# 6       f      p 0.87679916 0.65882239
# 7       g      q 0.94408782 0.07846614
# 8       h      r 0.41669714 0.18997531
# 9       i      s 0.35965571 0.90215068
# 10      j      t 0.64287793 0.84273345

Which columns are numeric?

sapply(mydata, is.numeric)

# alpha1 alpha2   num1   num2 
# FALSE  FALSE   TRUE   TRUE 

So, use this boolean vector to subset and you get just the numeric columns

my_num_data <- mydata[, sapply(mydata, is.numeric)]

#         num1       num2
# 1  0.5118055 0.82442771
# 2  0.3512970 0.12066818
# 3  0.4344065 0.94698653
# 4  0.1222383 0.72324135
# 5  0.1974004 0.51563337
# 6  0.2794483 0.06022451
# 7  0.1519816 0.32559160
# 8  0.5129894 0.76990432
# 9  0.2433832 0.08038982
# 10 0.7893464 0.45767856

now you can run cor()

cor(my_num_data, use = "complete.obs", method = "pearson")

#           num1      num2
# num1 1.0000000 0.2852567
# num2 0.2852567 1.0000000

And to show it as a one-liner:

cor(mydata[, sapply(mydata, is.numeric)],
    use = "complete.obs", method = "pearson")
like image 141
arvi1000 Avatar answered Oct 12 '22 22:10

arvi1000