Hello - I need to read in some tables that are embedded within data files like this: line 1 line 2 data table 01000 10110 00011 end table line 3 line 4 Is there any way to read just the data by telling an input device to start reading when it encounters the keyword "data table" and stop reading at "end table"? Thanks in advance, Jesse -- View this message in context: http://www.nabble.com/IO%3A-streaming-input-data-tp21646179p21646179.html Sent from the R help mailing list archive at Nabble.com.
Here is one way to do it:> x <- readLines(textConnection("line 1+ line 2 + data table + 01000 + 10110 + 00011 + end table + line 3 + line 4"))> start <- grep("data table", x) > end <- grep("end table", x) > # now read in only the data between limit > input <- read.table(textConnection(x[(start + 1):(end - 1)])) > closeAllConnections() > > inputV1 1 1000 2 10110 3 11>Uses 'textConnection' On Sat, Jan 24, 2009 at 5:48 PM, jesse daniel <jdlecy at gmail.com> wrote:> > Hello - > > I need to read in some tables that are embedded within data files like this: > > line 1 > line 2 > data table > 01000 > 10110 > 00011 > end table > line 3 > line 4 > > Is there any way to read just the data by telling an input device to start > reading when it encounters the keyword "data table" and stop reading at "end > table"? > > Thanks in advance, > Jesse > -- > View this message in context: http://www.nabble.com/IO%3A-streaming-input-data-tp21646179p21646179.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Hi Jesse, Here is another option by using a function, which argument is the file's name: # A function rmf<-function(filename){ x<-readLines(filename,warn=FALSE) res<-which(x=='data table' | x=='end table') x<-x[seq(res[1]+1,res[2]-1)] mydata<-read.table(textConnection(x)) closeAllConnections() mydata } As you see, I used some ideas from Jim Holtman's post (I hope you don't mind Jim). Here is how 'rmf' works:> rmf('ex.txt') # I saved the data you sent as 'ex.txt' keeping the samestructure V1 1 1000 2 10110 3 11 Now, if you have a lot of files with the same structure and you want to extract the same information, then using *apply you will be able to do the job. All you need is the name of them. HTH, Jorge On Sat, Jan 24, 2009 at 5:48 PM, jesse daniel <jdlecy@gmail.com> wrote:> > Hello - > > I need to read in some tables that are embedded within data files like > this: > > line 1 > line 2 > data table > 01000 > 10110 > 00011 > end table > line 3 > line 4 > > Is there any way to read just the data by telling an input device to start > reading when it encounters the keyword "data table" and stop reading at > "end > table"? > > Thanks in advance, > Jesse > -- > View this message in context: > http://www.nabble.com/IO%3A-streaming-input-data-tp21646179p21646179.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]