Here is a way -- you are dividing by zero in the scaling:
> x
sp wg n v.realtime v.cputime v.tcputime v.idletime v.nswtch
9 0 1 1 28.61300 28.61 28.6039 0.0000000 407
1563 0 1 2 15.20270 30.38 28.5981 0.9805230 483
3128 0 1 4 12.50930 50.00 28.6053 10.7877000 489
4682 0 1 6 12.10260 72.55 28.6193 22.0203000 488
6241 0 1 8 12.11510 96.80 28.6448 34.1126000 494
121 0 10 1 28.61530 28.60 28.6056 0.0000000 1996
1684 0 10 2 14.33530 28.66 28.6003 0.0296437 1908
3241 0 10 4 7.27129 29.05 28.6002 0.2313020 2110
4801 0 10 6 4.91522 29.42 28.6002 0.4259310 2134
6367 0 10 8 3.79337 30.25 28.6032 0.8424120
2245> x.s <- split(x, x[,1:2])
> x.result <- do.call(rbind, lapply(x.s, function(.sec){
+ # create matrix of data to scale
+ .mat <- as.matrix(.sec[, -(1:3)])
+ # need to transpose due to recycle of arguments
+ .mat <- t(t(.mat) / .mat[1,])
+ .sec[, -(1:3)] <- as.data.frame(.mat)
+ .sec
+ }))> x.result
sp wg n v.realtime v.cputime v.tcputime v.idletime v.nswtch
0.1.9 0 1 1 1.0000000 1.000000 1.0000000 NaN 1.0000000
0.1.1563 0 1 2 0.5313214 1.061866 0.9997972 Inf 1.1867322
0.1.3128 0 1 4 0.4371894 1.747641 1.0000489 Inf 1.2014742
0.1.4682 0 1 6 0.4229756 2.535827 1.0005384 Inf 1.1990172
0.1.6241 0 1 8 0.4234124 3.383432 1.0014299 Inf 1.2137592
0.10.121 0 10 1 1.0000000 1.000000 1.0000000 NaN 1.0000000
0.10.1684 0 10 2 0.5009663 1.002098 0.9998147 Inf 0.9559118
0.10.3241 0 10 4 0.2541050 1.015734 0.9998112 Inf 1.0571142
0.10.4801 0 10 6 0.1717689 1.028671 0.9998112 Inf 1.0691383
0.10.6367 0 10 8 0.1325644 1.057692 0.9999161 Inf
1.1247495>
>
On Mon, May 4, 2009 at 3:41 PM, Zeljko Vrba <zvrba@ifi.uio.no> wrote:
> 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
> }
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
>
http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
[[alternative HTML version deleted]]