[R] help with read.csv() for files with different number of columns

Fix Ace acefix at rocketmail.com
Sun Aug 27 22:25:39 CEST 2017


Dear R community,
I have a text file (test.txt) with different number of columns:
0610007P14Rik%%% Tcf19 Gtf2i 0610010O12Rik%%% Ivns1abp Etv6 1100001G20Rik%%% Nmi 1500015O10Rik%%% Foxi1 Ascl3 Sirt3 1700003E16Rik%%% Ascl2 Ifnar2 1700028J19Rik%%% Musk Nfe2l3 1810011O10Rik%%% Ppp1r13b Bpnt1 Cdkn2c Foxc1 Sox10 Smarca2 1810019D21Rik%%% Asb8 1810037I17Rik%%% Zfp612 1810055G02Rik%%% Nkx2-3 Maged1 Runx1 Ugp2 Elk4 Spdef Tcf19 Isl2 Gtf2i Ctnnbl1 Tcea3 Ank2 Zfp612 Creb3l1 Nupr1 3632451O06Rik Creb3l4 Lass6 
I wold like to read it into R using
 > test=read.csv("test.txt",sep="\t",header=FALSE)
However, when I check the r object "test", I found that all the rows have 5 columns:
> test                 V1            V2      V3     V4      V51  0610007P14Rik%%%         Tcf19   Gtf2i               2  0610010O12Rik%%%      Ivns1abp    Etv6               3  1100001G20Rik%%%           Nmi                       4  1500015O10Rik%%%         Foxi1   Ascl3  Sirt3        5  1700003E16Rik%%%         Ascl2  Ifnar2               6  1700028J19Rik%%%          Musk  Nfe2l3               7  1810011O10Rik%%%      Ppp1r13b   Bpnt1 Cdkn2c   Foxc18             Sox10       Smarca2                       9  1810019D21Rik%%%          Asb8                       10 1810037I17Rik%%%        Zfp612                       11 1810055G02Rik%%%        Nkx2-3  Maged1  Runx1    Ugp212             Elk4         Spdef   Tcf19   Isl2   Gtf2i13          Ctnnbl1         Tcea3    Ank2 Zfp612 Creb3l114            Nupr1 3632451O06Rik Creb3l4  Lass6 
Basically it breaks some rows into more than one rows. For example, row 7 in the original record becomes two rows. Looks like the "test" always has 5 columns. 
How does this happen? How should I fix it to make one record into one two in R object?
Thank you very much!
Ace




 


   
	[[alternative HTML version deleted]]



More information about the R-help mailing list