All, I have a 30 million record text file without header information. I would like to add a header to this file without reading it first. Is this possible? The code below does what I want except that the readLines portion takes quite a long time. Is there a way around reading the lines? I'm working on Windows XP. Zev input<-readLines("c:/junk/forR.csv") input<-c(c('"a", "b", "c", "d", "e", "f"'), input) writeLines(input, "c:/junk/forRfix.csv") -- Zev Ross ZevRoss Spatial Analysis 120 N Aurora, Suite 3A Ithaca, NY 14850 607-277-0004 (phone) 866-877-3690 (fax, toll-free) zev at zevross.com
If you want to place the header at the beginning of the file, then you have to read/write the entire file to get that first line in. On Thu, Apr 15, 2010 at 10:34 AM, Zev Ross <zev@zevross.com> wrote:> All, > > I have a 30 million record text file without header information. I would > like to add a header to this file without reading it first. Is this > possible? The code below does what I want except that the readLines portion > takes quite a long time. Is there a way around reading the lines? I'm > working on Windows XP. > > Zev > > input<-readLines("c:/junk/forR.csv") > input<-c(c('"a", "b", "c", "d", "e", "f"'), input) > writeLines(input, "c:/junk/forRfix.csv") > > -- > Zev Ross > ZevRoss Spatial Analysis > 120 N Aurora, Suite 3A > Ithaca, NY 14850 > 607-277-0004 (phone) > 866-877-3690 (fax, toll-free) > zev@zevross.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<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? [[alternative HTML version deleted]]
You also might take a look at using file.append to speed up the process. On Thu, Apr 15, 2010 at 10:34 AM, Zev Ross <zev@zevross.com> wrote:> All, > > I have a 30 million record text file without header information. I would > like to add a header to this file without reading it first. Is this > possible? The code below does what I want except that the readLines portion > takes quite a long time. Is there a way around reading the lines? I'm > working on Windows XP. > > Zev > > input<-readLines("c:/junk/forR.csv") > input<-c(c('"a", "b", "c", "d", "e", "f"'), input) > writeLines(input, "c:/junk/forRfix.csv") > > -- > Zev Ross > ZevRoss Spatial Analysis > 120 N Aurora, Suite 3A > Ithaca, NY 14850 > 607-277-0004 (phone) > 866-877-3690 (fax, toll-free) > zev@zevross.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<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? [[alternative HTML version deleted]]
if i were you, i probably will use 1 line of sed to do such task instead of R to insert headers in the file. On Thu, Apr 15, 2010 at 10:34 AM, Zev Ross <zev at zevross.com> wrote:> All, > > I have a 30 million record text file without header information. I would > like to add a header to this file without reading it first. Is this > possible? The code below does what I want except that the readLines portion > takes quite a long time. Is there a way around reading the lines? I'm > working on Windows XP. > > Zev > > input<-readLines("c:/junk/forR.csv") > input<-c(c('"a", "b", "c", "d", "e", "f"'), input) > writeLines(input, "c:/junk/forRfix.csv") > > -- > Zev Ross > ZevRoss Spatial Analysis > 120 N Aurora, Suite 3A > Ithaca, NY 14850 > 607-277-0004 (phone) > 866-877-3690 (fax, toll-free) > zev at zevross.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. >-- =============================WenSui Liu Blog : statcompute.spaces.live.com Tough Times Never Last. But Tough People Do. - Robert Schuller