[R] to the person who asked about dividing by the base row

markleeds at verizon.net markleeds at verizon.net
Mon May 4 22:40:02 CEST 2009


   to whomever that was. i deleted your email but I think below does what you
   want. as always, there's probably some
   improvement that could be done. whether it's  minor or major, i'm not sure
   ?
   also, 3 things:Â Â
   A)there are some Inf's in the output because of division by zero but I
   wasn't sure how you wanted to handle that.
   B) it also assumes that an n=1 always exists and you should probably have
   some error checking in there for when it doesn't.Â
   C) When i tried to condense it to below, I got an error that division is
   only valid for equal sized data frames and
   I'm not sure why so maybe someone can say something about that ?
   temp <- lapply(split(DF,list(DF$sp,DF$wg)), function(.df) {
   Â Â Â     .df[(.df$n    !=    1),c(3,4,5,6,7)]    <-   .df[(.df$n   !=
   1),c(3,4,5,6,7)]/.df[(.df$n == 1),c(3,4,5,6,7)]
   Â Â Â  .df
   })
   # WORKING CODE
   #===========================================================================
   Â
   DF <- read.table(textConnection("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"),header=TRUE)

   print(DF)

   temp <- lapply(split(DF,list(DF$sp,DF$wg)), function(.df) {
   Â Â Â    .df$v.realtime[.df$n   !=   1]   <-  .df$v.realtime[.df$n  !=
   1]/.df$v.realtime[.df$n == 1]
   Â Â Â    .df$v.cputime[.df$n   !=   1]   <-   .df$v.cputime[.df$n   !=
   1]/.df$v.cputime[.df$n == 1]
         .df$v.tcputime[.df$n   !=  1]  <-  .df$v.tcputime[.df$n   !=
   1]/.df$v.tcputime[.df$n == 1]
   Â Â Â    .df$v.idletime[.df$n   !=   1]   <-  .df$v.idletime[.df$n  !=
   1]/.df$v.idletime[.df$n == 1]
   Â Â Â     .df$v.nswtch[.df$n    !=   1]   <-   .df$v.nswtch[.df$n   !=
   1]/.df$v.nswtch[.df$n == 1]
   Â Â Â  .df
   })

   result <- do.call(rbind,temp)
   rownames(result) <- NULL
   print(result)



More information about the R-help mailing list