0

I want to have a loop that will perform a calculation for me, and export the variable (along with identifying information) into a new data frame.

My data look like this:

Each unique sampling point (UNIQUE) has 4 data points associated with it (they differ by WAVE).

   WAVE REFLECT REFEREN PLOT LOCAT COMCOMP     DATE UNIQUE
1 679.9     119       0    1     1       1 11.16.12      1
2 799.9     119       0    1     1       1 11.16.12      1
3 899.8     117       0    1     1       1 11.16.12      1
4 970.3     113       0    1     1       1 11.16.12      1
5 679.9     914   31504    1     2       1 11.16.12      2
6 799.9    1693   25194    1     2       1 11.16.12      2

And I want to create a new data frame that will look like this: For each unique sampling point, I want to calculate "WBI" from 2 specific "WAVE" measurements.

WBI                     PLOT   ....  UNIQUE
(WAVE==899.8/WAVE==970)    1              1
(WAVE==899.8/WAVE==970)    1              2
(WAVE==899.8/WAVE==970)    1              3
Zombo
  • 1
  • 62
  • 391
  • 407
  • 5
    Welcome to StackOverflow. I would warn you that while people here are helpful, they will most likely ask you, [what have you tried?](http://whathaveyoutried.com) Providing some small amount of code which you have tried would greatly help us help you. – sebastian-c Feb 05 '13 at 06:00
  • You may want to study the following question/answer (http://stackoverflow.com/questions/13475039/how-to-optimize-the-following-code-with-nested-while-loop-multicore-an-option). This may give you some insights in how to work with loops. Good luck! – Jochem Feb 05 '13 at 07:14

1 Answers1

0

Depends on the size of your input data.frame there could be better solution in terms of efficiency but the following should work ok for small or medium data sets, and is kind of simple:

out.unique = unique(input$UNIQUE);

out.plot = sapply(out.unique,simplify=T,function(uq) {
    # assuming that plot is simply the first PLOT of those belonging to that
    # unique number. If not yo should change this.
    subset(input,subset= UNIQUE == uq)$PLOT[1];
});

out.wbi = sapply(out.unique,simplify=T,function(uq) {
    # not sure how you compose WBI but I assume that are the two last 
    # record with that unique number so it matches the first output of your example
    uq.subset = subset(input,subset= UNIQUE == uq);
    uq.nrow = nrow(uq.subset);
    paste("(WAVE=",uq.subset$WAVE[uq.nrow-1],"/WAVE=",uq.subset$WAVE[uq.nrow],")",sep="")
});

output = data.frame(WBI=out.wbi,PLOT=out.plot,UNIQUE=out.unique);

If the input data is big however you may want to exploit de fact that records seem to be sorted by "UNIQUE"; repetitive data.frame sub-setting would be costly. Also both sapply calls can be combined into one but make it a bit more cumbersome so I had leave it like this.

Valentin Ruano
  • 2,726
  • 19
  • 29