Julio Diaz
Julio Diaz

Reputation: 9437

Reading csv table in R

I have a csv file that I am trying to read in R, for some reason R thinks the first row is the header for each column which is not true. Is there a way to get around this. This is my command:

test <- as.matrix(read.csv(file="filetable.csv", sep=",", header=FALSE)) 

I get something like this

      AATCAGGC   X25070  
 [1,] "ACAAGGCT" " 50687"
 [2,] "ACACGATC" " 47483"
 [3,] "ACACTGAC" " 18339"
 [4,] "ACAGGAGT" " 48550"

the first row should be part of the data

Opening filetable.csv in notepad I get this

AATCAGGC,25070
ACAAGGCT,50687
ACACGATC,47483
ACACTGAC,18339
ACAGGAGT,48550

Thanks

Upvotes: 2

Views: 12721

Answers (1)

Hansi
Hansi

Reputation: 2644

It's header=FALSE not head: http://stat.ethz.ch/R-manual/R-patched/library/utils/html/read.table.html

> tempFile <- tempfile()
> writeLines(c("AATCAGGC,25070",
 "ACAAGGCT,50687",
 "ACACGATC,47483",
 "ACACTGAC,18339",
 "ACAGGAGT,48550"),tempFile,sep="\n")
> readLines(tempFile)
[1] "AATCAGGC,25070" "ACAAGGCT,50687" "ACACGATC,47483" "ACACTGAC,18339" "ACAGGAGT,48550"
> as.matrix(read.csv(tempFile,sep=",",header=FALSE))
     V1         V2     
[1,] "AATCAGGC" "25070"
[2,] "ACAAGGCT" "50687"
[3,] "ACACGATC" "47483"
[4,] "ACACTGAC" "18339"
[5,] "ACAGGAGT" "48550"

Upvotes: 4

Related Questions