[R] iteration introspection?

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Sat Aug 7 11:53:41 CEST 2004


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 ------------------------------




More information about the R-help mailing list