Wednesday, June 10, 2015

How to load file with multiple spaces in R

In R if you try to load file with multiple spaces as a delimiter using sep=" " (single space) you may get:

ex <- read.delim("ex.1.merge.1", sep=" ", header=T)

Error in read.table(file = file, header = header, sep = sep, quote = quote,  :
  more columns than column names


Instead use "" that will mark any number of spaces, unlike " " that is a single space.

ex <- read.delim("ex.1.merge.1", sep="", header=T)
> head(ex)
             GSM1289335 GSM1289336 GSM1289337 GSM1289338 GSM1289339 GSM1289340
1415670_at      8.24126    8.52633    8.33998    8.86572    8.90274    8.70942
1415671_at     11.13090   11.24580   11.17340   10.94100   11.20140   11.04830
1415672_at     11.45830   11.44230   11.46260   11.18010   11.28670   11.21400
1415673_at      6.88159    6.98538    6.82758    6.41075    6.20351    6.48295
1415674_a_at    9.48842    9.45669    9.44673    9.92935    9.85057    9.86870
1415675_at      8.83436    8.97068    8.87163    9.22176    9.44220    9.33622
             GSM1289341 GSM1289342 GSM1289343    affy_mouse430_2
1415670_at      8.43722    8.57212    8.62239 ENSMUSG00000030058
1415671_at     11.30660   11.25010   11.37650 ENSMUSG00000013160
1415672_at     11.30770   11.28530   11.28260 ENSMUSG00000015341
1415673_at      6.64169    6.77523    6.87241 ENSMUSG00000029446
1415674_a_at    9.74287    9.70161    9.85818 ENSMUSG00000032112
1415675_at      8.85549    8.97766    9.03381 ENSMUSG00000026810
             ensembl_gene_id
1415670_at        1415670_at
1415671_at        1415671_at
1415672_at        1415672_at
1415673_at        1415673_at
1415674_a_at    1415674_a_at
1415675_at        1415675_at

No comments:

Post a Comment