Bade
Bade

Reputation: 747

Using read.table to read a CSV file gives a table with quotes

I am trying to read a csv file in R using the read.table command and the table I get in R has double quotes around every entry. The problem is that I cannot use these entries with quotes to do mathematical operations.

Here is my read command:

exprs_data <- as.matrix(read.table("Test1.csv",
     sep= ",",header=TRUE,row.names=1,as.is=TRUE))##

Here is the imported table in R:

ABC DEF XYZ 
m0122 " 854"    "1487"  "1855"
m0152 "  97"    " 159"  " 468"
m0257 " 157"    " 733"  "   6"

Why are there quotes around numbers?? I never experienced this problem in R before. Can anybody help me importing this csv file in R?

Upvotes: 1

Views: 6233

Answers (1)

sebkopf
sebkopf

Reputation: 2375

The quotes indicate that the values in your matrix are strings rather than numbers. Without knowing what your csv file looks like, I suspect some value in the file is not a valid number, and with the conversion to a matrix (your as.matrix) statement, everything is converted to strings to comply with the required structure of the matrix (needs to be all the same data type). I'm not entirely sure why you are doing the matrix conversion but you can explicitly specify in read.table what type the data is by using the colClasses parameter. Try this (assuming all columns are to be treated as numbers, otherwise use a vector of different values for each column in colClasses):

exprs_data <- read.table("Test1.csv", sep= ",", header=TRUE, 
                         row.names=1, colClasses = "numeric")

You can convert it to a matrix if you want but you could also work directly with the data frame that read.table returns. As mentioned in the comment, you can get details on the structure (including column data types) of your variable by running str(exprs_data).

Upvotes: 3

Related Questions