[R] data which has different size of elements in each row

Marc Schwartz marc_schwartz at comcast.net
Wed Nov 12 19:23:00 CET 2008


This was the scenario that Phoebe posted about. Her data was:

2.93290e-06 1.17772e-06 -0.645205  rs2282755
3.07521e-06 3.14000e-04  0.412997  rs1336838
4.84017e-06 2.18311e-01  0.188669  rs2660664 rs967785
9.77861e-06 7.04740e-02  0.294653  rs2660664
1.22767e-05 1.56325e-05  0.569826  rs6870519
2.27205e-05 1.89000e-04 -0.472862 rs10488345

where the first row has one less column than the third row.

Be sure that you are specifying the function arguments correctly for
your data. Importantly, note the following from the Details section of
?read.table:

The number of data columns is determined by looking at the first five
lines of input (or the whole file if it has less than five lines), or
from the length of col.names if it is specified and is longer. This
could conceivably be wrong if fill or blank.lines.skip are true, so
specify col.names if necessary.

Marc

on 11/12/2008 09:49 AM yanniliu wrote:
> I have the same problem. If the first row has more columns than later rows,
> the fill=TRUE works; however when the first row has LESS columns than later
> rows, R won't read in more columns than the length of the first row. Anyone
> has solutions?
> 
> Yanni 
> 
> Marc Schwartz wrote:
>> on 11/11/2008 03:39 PM phoebe kong wrote:
>>> Hi all,
>>>
>>> I have problem reading in a text file as follow.
>>>
>>> The following data has not column header. I tried the following
>>> command but failed,
>>>
>>> temp<-read.table("data.txt",header=F)
>>>
>>> An error message stated that line 3 did not have 4 elements.
>>>
>>> 0.293290E-05       0.117772E-05     -0.645205     rs2282755
>>>  0.307521E-05           0.000314      0.412997     rs1336838
>>>  0.484017E-05           0.218311      0.188669     rs2660664        
>>> rs967785
>>>  0.977861E-05           0.070474      0.294653     rs2660664
>>>  0.122767E-04       0.156325E-04      0.569826     rs6870519
>>>  0.227205E-04           0.000189     -0.472862     rs10488345
>>>
>>> Does anyone know how to solve it?
>>>
>>> Thanks in advance for your help.
>>>
>>> Sit
>>>
>> See the 'fill' argument in ?read.table
>>
>>> read.table("clipboard", header = FALSE, fill = TRUE)
>>            V1          V2        V3         V4       V5
>> 1 2.93290e-06 1.17772e-06 -0.645205  rs2282755
>> 2 3.07521e-06 3.14000e-04  0.412997  rs1336838
>> 3 4.84017e-06 2.18311e-01  0.188669  rs2660664 rs967785
>> 4 9.77861e-06 7.04740e-02  0.294653  rs2660664
>> 5 1.22767e-05 1.56325e-05  0.569826  rs6870519
>> 6 2.27205e-05 1.89000e-04 -0.472862 rs10488345
>>
>>
>> HTH,
>>
>> Marc Schwartz
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>



More information about the R-help mailing list