Michael Zhuoyu
Michael Zhuoyu

Reputation: 33

'x' must be numeric Traceback: for computing correlation for a matrix

I want to compute the correlation between each column within a matrix. But it throw an error: "Error in cor(sample, use = "pairwise.complete.obs"): 'x' must be numeric Traceback:

  1. cor(sample, use = "pairwise.complete.obs")
  2. stop("'x' must be numeric")"

This is what I did:

data = read.csv("mail.csv", header=F)

sample = data[-(1),-(1)]

cor(sample, use="pairwise.complete.obs")

My data set looks like this: I would appreciate if someone could tell me where I did wrong? enter image description here

Upvotes: 2

Views: 27170

Answers (1)

xyz123
xyz123

Reputation: 651

Run

str(data) to look at your data types of your columns - example: numeric, factor, integer. See which column is not numeric which should be, and then ... I'm not very good about referring to columns in a matrix so put it into a dataframe. Rename your column, and take it back into a matrix.

data = as.data.frame(data)

data$column = as.numeric(data$column)
where "column" represents your issue column.  

data = as.matrix(data) 

There now you can rerun your code.

Upvotes: 5

Related Questions