1

I want to read the csv file as a matrix, and the matrix is numeric. The followwing is my code

1, read the data

mydata<-read.csv("mydata.csv", header = TRUE, sep=',', check.names = FALSE)
str(mydata)

2, transform as numeric data

mydata_1<-data.matrix(mydata)
str(mydata_1)

For the first step, the output is finesee pic1

but for the second step, when I want to transform the dataframe as a numeric matrix, the output is changed to be as follows. See that the rownames and the column names have been changed, and the cell values are also not true, which are not wanted. see pic2

I also tried the following syntax, but it produced the same result as above.

mydata_1<-sapply(mydata, as.numeric)

and here is the link of my data file link of mydata

Any suggestion is appreciated.

Community
  • 1
  • 1
statistics_learning
  • 427
  • 1
  • 4
  • 14
  • 5
    My guess (and I'm not going to download your data. You should make your question [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) without a link to external data) is that your data were factors to begin with. – Heroka Jan 13 '16 at 20:44
  • 2
    Have you tried `read.csv(...,na.strings = "--")`? – Marat Talipov Jan 13 '16 at 20:46
  • 2
    You might also try `row.names = 1` in your `read.csv()` call, to make that first column into the row names. And a side note: `header = TRUE` and `sep = ","` are the default values in `read.csv()`. You don't need to write them. – Rich Scriven Jan 13 '16 at 20:46
  • Try this: `as.matrix(read.csv("mydata.csv", row.names = 1, check.names = FALSE))` – Rich Scriven Jan 13 '16 at 21:00
  • 1
    Thank you @Marat Talipov. combining you and Richard's suggestion, it perfectly work! Thank you so much! – statistics_learning Jan 13 '16 at 21:06
  • 2
    Thank you @RichardScriven, it works by adding ' row.names = 1' and 'na.strings = "--" '. Thank you so much for your help! – statistics_learning Jan 13 '16 at 21:08

1 Answers1

3

This is because you have several data types (factor, num), and a matrix can only store one single data type. I'd suggest you remove the first column to have only numerics:

mydata_1<-data.matrix(mydata[,-1])
HubertL
  • 19,246
  • 3
  • 32
  • 51
  • 1
    Thank you @HubertL , but I need the country names as the row names, so I can't remove it. This is the first problem, and the second problem is that the ` data.matrix() ` function will change the magnitude of data value, which is not wanted. – statistics_learning Jan 13 '16 at 20:55
  • why do you want to use matrix? – HubertL Jan 13 '16 at 20:59
  • 1
    Thank you @HubertL. You are right. The problem truely lies in the rownames, so by adding 'rowname= 1', the problem is solved. – statistics_learning Jan 13 '16 at 21:10