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

25,295

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.

Share:
25,295
Michael Zhuoyu
Author by

Michael Zhuoyu

Updated on February 21, 2020

Comments

  • Michael Zhuoyu
    Michael Zhuoyu about 4 years

    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