[R] Ignore text when reading data

beyar bxx at mailinator.com
Thu Jan 29 05:09:59 CET 2009


thanks to all for the solutions.  Especially to Jim H for this one which
worked perfectly...
(i only had to change the seperater on the header to /t as there are spaces
in header names)




----------------
Try this:

> x <- readLines(textConnection("main data file - file 1
+ by mr x
+ etc
+
+
+ Time    out1
+ Sec     mm
+ 0.82495117      -0.020977303
+ 1.3554688       -0.059330709
+ 1.826416        -0.021419302
+ 2.3295898       -0.051521059
+ 2.8347168       -0.020661414
+
+
+ Time    out1
+ Sec     mm
+ 3.8679199       -0.000439643
+ 4.3322754       -0.063477799
+ 4.8015137       -0.024581354
+ 5.3286133       -0.067487299
+ 5.8212891       -0.011978489"))
> closeAllConnections()
> # remove blanks
> x <- x[x != ""]
> # get the lines with numbers
> indx.num <- grep("^[-0-9]", x)
> header <- x[indx.num[1] - 2]
> input <- read.table(textConnection(x[indx.num]))
> names(input) <- strsplit(header, "\\s+")[[1]]
> input


beyar wrote:
> 
> Hi,
> I have tab delimited text files containing numerical data,
> like below, but many more columns.
> 
> As you can see, the first few lines are heading and file data.  I need to
> skip these lines.  2 lines above where the numbers start is what I want to
> use as my header rows.  I then want to ignore the next line (containing
> units) and start importing data.  
> 
> The header row repeats.  i want to ignore the blank rows and text in the
> data and then continue reading.  Is there an easy way to do this?  
> 
> thanks
> Beyar
> 
> 
> -------------------------
> main data file - file 1
> by mr x
> etc
> 
> 
> Time	out1	
> Sec	mm
> 0.82495117	-0.020977303
> 1.3554688	-0.059330709
> 1.826416	-0.021419302
> 2.3295898	-0.051521059
> 2.8347168	-0.020661414
> 
> 
> Time	out1	
> Sec	mm
> 3.8679199	-0.000439643
> 4.3322754	-0.063477799
> 4.8015137	-0.024581354
> 5.3286133	-0.067487299
> 5.8212891	-0.011978489
> 
> -----------------------------------------------
> 
> 

-- 
View this message in context: http://www.nabble.com/Ignore-text-when-reading-data-tp21718709p21720588.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list