0

I'm working on a project for uni and I am trying to calculate relatedness between different products and industries. I have created an matrix in excel, which has the export from European countries per HS2 code. I converted this to a csv file and tried to import it to R. However, R reads it as characters instead of numeric, which I need to calculate. I have searched for many answers and tried multiple things, however nothing seems to work. Hopefully somebody can help me out! I used

str(data,class)

And indeed, all of my columns are read as chr instead of int

The code I used to import my data is

data=read.csv("matrixproject.csv",sep=";",row.names=1,header=T) 
matrix=as.matrix(data)

I have tried to use the following code

data_num <- as.data.frame(apply(data, 2, as.numeric))

However this changes all my values to NA, so it does not help. Does anyone have any idea how to change this? As I can not do any further work on my project until this is solved. Thank you in advance!

Phil
  • 7,287
  • 3
  • 36
  • 66
  • Take a look at `read.csv2` (since your data is semi-colon separated). You should also look into the argument `colClasses`. Otherwise, without an example of `matrixproject.csv`, it is difficult to help you. See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – MrGumble Jan 07 '21 at 12:08
  • Thank you! I will look into this! I was able to solve it by rounding all my numbers up in excel, so that none had decimals anymore. After that change, my numbers were read as actual numbers! – Gaby van 't Hart Jan 07 '21 at 13:53
  • Sounds as if your csv data had a mystery decimal separator, perhaps "," (which is the default for `read.csv2`). – MrGumble Jan 07 '21 at 14:48

0 Answers0