I'm trying to read in a file using the function fread. The file that I'm trying to read in has about 100 lines of information I don't want prior to getting to my matrix of data that I do want. On the line prior to the data I want there is always a string identifier "*end*" The following fread call: impcoord <- fread('H:/SBE19plus_01907535_2015_06_17_0093.cnv',skip="*END*") almost gets me there but it starts reading AT *END* and I'd like to have it start the line after. I can't figure out how to make this work. I know I could have a two step function where I have a function scan the file to Identify the line that has *END* but I thought I could just do it with one fread() call. thanks for the help. Trevor [[alternative HTML version deleted]]
Hi Trevor, I couldn't work out how to do it with just fread, but perhaps this will help: # create a test file sink("test_fread.dat") cat("This is the header of a file\n") cat("that ends with the word *end*\n") cat("Col1 Col2\n1 2\n3 4\n5 6\n") sink() # try to read it test_con<-file("test_fread.dat","rt") header_lines<-1 while(header_lines) { nextline<-readLines(test_con,1) header_lines<-length(grep("*end",nextline,fixed=TRUE))==0 } fread_dat<-read.table(test_con,header=TRUE) Jim On Sat, Jun 27, 2015 at 2:16 AM, Trevor Davies <davies.trevor at gmail.com> wrote:> I'm trying to read in a file using the function fread. > > The file that I'm trying to read in has about 100 lines of information I > don't want prior to getting to my matrix of data that I do want. On the > line prior to the data I want there is always a string identifier "*end*" > > The following fread call: > > impcoord <- fread('H:/SBE19plus_01907535_2015_06_17_0093.cnv',skip="*END*") > > almost gets me there but it starts reading AT *END* and I'd like to have it > start the line after. I can't figure out how to make this work. > > I know I could have a two step function where I have a function scan the > file to Identify the line that has *END* but I thought I could just do it > with one fread() call. > > thanks for the help. > Trevor > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hi Jim, Thanks,Jim. I couldn't get fread to work either for this. I ended up with this: txt<- readLines('test_fread.dat'); data.frame1 <- read.table(text=txt, sep='', header=FALSE, skip=grep('*END*', txt)) BTW, i use that Map Rose function you wrote a few years back quite often. Thanks for the contributions! Trevor On Sun, Jun 28, 2015 at 2:21 AM, Jim Lemon <drjimlemon at gmail.com> wrote:> Hi Trevor, > I couldn't work out how to do it with just fread, but perhaps this will > help: > > # create a test file > sink("test_fread.dat") > cat("This is the header of a file\n") > cat("that ends with the word *end*\n") > cat("Col1 Col2\n1 2\n3 4\n5 6\n") > sink() > # try to read it > test_con<-file("test_fread.dat","rt") > header_lines<-1 > while(header_lines) { > nextline<-readLines(test_con,1) > header_lines<-length(grep("*end",nextline,fixed=TRUE))==0 > } > fread_dat<-read.table(test_con,header=TRUE) > > Jim > > On Sat, Jun 27, 2015 at 2:16 AM, Trevor Davies <davies.trevor at gmail.com> > wrote: > > I'm trying to read in a file using the function fread. > > > > The file that I'm trying to read in has about 100 lines of information I > > don't want prior to getting to my matrix of data that I do want. On the > > line prior to the data I want there is always a string identifier "*end*" > > > > The following fread call: > > > > impcoord <- > fread('H:/SBE19plus_01907535_2015_06_17_0093.cnv',skip="*END*") > > > > almost gets me there but it starts reading AT *END* and I'd like to have > it > > start the line after. I can't figure out how to make this work. > > > > I know I could have a two step function where I have a function scan the > > file to Identify the line that has *END* but I thought I could just do it > > with one fread() call. > > > > thanks for the help. > > Trevor > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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]]