[R] Calculating relative ratios in a data frame..

Zeljko Vrba zvrba at ifi.uio.no
Mon May 4 21:41:13 CEST 2009


I have a data-set that is structured as follows:

         sp    wg    n v.realtime v.cputime v.tcputime  v.idletime v.nswtch
9         0     1    1   28.61300     28.61    28.6039 0.00000e+00      407
1563      0     1    2   15.20270     30.38    28.5981 9.80523e-01      483
3128      0     1    4   12.50930     50.00    28.6053 1.07877e+01      489
4682      0     1    6   12.10260     72.55    28.6193 2.20203e+01      488
6241      0     1    8   12.11510     96.80    28.6448 3.41126e+01      494
121       0    10    1   28.61530     28.60    28.6056 0.00000e+00     1996
1684      0    10    2   14.33530     28.66    28.6003 2.96437e-02     1908
3241      0    10    4    7.27129     29.05    28.6002 2.31302e-01     2110
4801      0    10    6    4.91522     29.42    28.6002 4.25931e-01     2134
6367      0    10    8    3.79337     30.25    28.6032 8.42412e-01     2245

[And a lot of other variables, and sp also changes]

Now, I want to divide each of the v.* variables for every (sp, wg, n)
combination by the values in the row defined by (sp, wg, 1), thus giving 
a relative change to the case n==1 for the same combination of sp and wg.
I have written the following function, but I'm wondering whether there's
an easier/more efficient way..  Perhaps by organizing the data structure
differently, and if so -- how?

speedup.1 <- function(df)
{
  groups <- split(df, df[,c("sp","wg")])
  ret <- list()

  for(g in groups) {
    ref <- g[1,]
    for(v in 4:15)  # sp,wg,n are factors, so / doesn't work..
      g[[v]] <- ref[[v]] / g[[v]]
    ret <- rbind(ret, g)
  }
  ret
}



More information about the R-help mailing list