Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string lines and make a data frame

Tags:

string

r

I have a small data from a text file read via readLines. The file had characters like # and I think that is the reason read.table was unable to read it. Here is the first five lines dput:

files<-c("\trfinal\t\t", "eq1\t\t\t", "0.ster6\t1.00\t(1.00,1.00)\t.", 
     "1.ster6\t0.65\t(0.47,0.88)\t0.006", "0.parkinson\t1.00\t(1.00,1.00)\t.", 
     "1.ster6#0.parkinson\t1.00\t(1.00,1.00)\t.")

\t means white space between strings. I would like to split this text lines and put them into a 4 column grid (data frame).

I tried strsplit(files, "[\\t]") but it doesn't really do the trick. Any help ?

like image 381
JeanVuda Avatar asked Jan 07 '15 09:01

JeanVuda


1 Answers

You can disable the treatment of # as the comment.char in read.table:

read.table(text=files, sep='\t', comment.char="")
#                    V1     V2          V3    V4
# 1                     rfinal
# 2                 eq1
# 3             0.ster6   1.00 (1.00,1.00)     .
# 4             1.ster6   0.65 (0.47,0.88) 0.006
# 5         0.parkinson   1.00 (1.00,1.00)     .
# 6 1.ster6#0.parkinson   1.00 (1.00,1.00)     .
like image 195
musically_ut Avatar answered Sep 22 '22 17:09

musically_ut