Hi everyone, I want to perform a regex substitution on line #1 in a file based on the contents of line #2. same is true for line 11 and line 12 etc... With the look at each line of a file rolling forward method it seems to me that I will not be able to use iterators like 'each' for this operation unless I am able to manipulate or even know of the position of the file pointer from within the iterator block but I don't know of a way to do this. Does anyone know how I can learn what the value of my iterator is within a loop? Thanks, Cere
On Fri, 6 Aug 2004, Cere Davis wrote:> I want to perform a regex substitution on line #1 in a file based on the > contents of line #2. same is true for line 11 and line 12 etc... > > With the look at each line of a file rolling forward method it seems to > me that I will not be able to use iterators like 'each' for this > operation unless I am able to manipulate or even know of the position of > the file pointer from within the iterator block but I don't know of a > way to do this.In R, we use connections to access files and the function seek() will tell you where you are on the file and move you elsewhere, separately for read and for write.> Does anyone know how I can learn what the value of my iterator is within > a loop?The iterator is `i' in for(i in 1:n), so you can access it directly. I suspect that was not the Q you wanted to ask. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
I am looking to attend an R course this summer in Atlanta area. Any help? Thanks alot Eugene
see attached link http://tolstoy.newcastle.edu.au/R/help/04/07/1491.html Mihai Nica Jackson State University 155 B Parkhurst Dr. Jackson, MS 39202 601 969 5423 ----- Original Message ----- From: "eugene dalt" <eugenedalt at yahoo.com> To: <r-help at stat.math.ethz.ch> Sent: Friday, August 06, 2004 2:27 PM Subject: [R] Looking for R course in Atlanta> I am looking to attend an R course this summer > in Atlanta area. Any help? > > Thanks alot > Eugene > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html>
On 06-Aug-04 Cere Davis wrote:> > Hi everyone, > > I want to perform a regex substitution on line #1 in a file > based on the contents of line #2. same is true for line 11 > and line 12 etc... > > With the look at each line of a file rolling forward method > it seems to me that I will not be able to use iterators like > 'each' for this operation unless I am able to manipulate or > even know of the position of the file pointer from within the > iterator block but I don't know of a way to do this. > > Does anyone know how I can learn what the value of my iterator > is within a loop?It's not clear from your description why you are thinking of using R for this. Normally, in the situation described in your first paragraph, I would run the file (in plain text format of course) through 'awk', with the 'awk' program written so as to delay the output of line n (suitably modified) until line (n+1) had been read, enabling the computation of the modification to line n. You can certainly keep track of the value of n, so that (e.g. as you describe) you only do it for n = 1, 11, 21, ... One argument for using 'awk', where possible, is that it has very comprehensive resources for handling regular expressions, which your description implies a need for. However, doing it this way would need the substitution to be sufficiently straightforward to compute that you could do it using the resources of 'awk'. If the computation needed the resources of some sophisticated function in R (even if only something like perturbing the value of a variable in line n by adding a random deviate from a non-central t with non centrality parameter determined from line (n+1)), then I guess you would need to do it inside R. But in that sort of case, what's wrong with reading in the original file to become a dataframe in R, and explicitly looping through the lines of the dataframe using a variable say "i", whose value would always be accessible within the loop? For instance, if "DF" is the name of the dataframe, then DF[i,] is row i of DF, and DF has nrow(DF) rows of data. So your loop could be like for(i in (1:nrow(DF)-1)){ if(i%%10 == 1){ look at DF[i+1,] and then do whatever you need to do with DF[i,] } } Then you can write out the modified DF to a file using, say, 'write.table'. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 07-Aug-04 Time: 10:53:41 ------------------------------ XFMail ------------------------------
Cere Davis <cere <at> u.washington.edu> writes:> > Hi everyone, > > I want to perform a regex substitution on line #1 in a file based on the > contents of line #2. same is true for line 11 and line 12 etc... > > With the look at each line of a file rolling forward method it seems to > me that I will not be able to use iterators like 'each' for this > operation unless I am able to manipulate or even know of the position of > the file pointer from within the iterator block but I don't know of a > way to do this. > > Does anyone know how I can learn what the value of my iterator is within > a loop?Assuming you want to process the lines in pairs read them all in and then loop: lines <- readLines("my.txt") n <- seq(along = lines) for( i in seq(1,n,2)) { # process line[i] and line[i+1] ... } Or if you want to read them in a pair at a time: con <- file("my.txt", "r") while(length(line <- readLines(con, 1))) { next.line <- readLines(con, 1) stopifnot(length(next.line)) # process line and next.line } close(con) If you want to read them in one by one rather than in pairs then have a look at ?pushBack