and retains the
grouping column (the first) while your original
code returns a matrix without the grouping column.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Bert Gunter
Sent:
y(names.used, function(x){
>>> unlist(by(frame, frame$group, function(y) y[,x] /
>>> mean(y[,x],na.rm=T)))
>>> }))
>>> })
>>> # Took me 1.25-1.32 min
>>> ###############
>>>
>>> ### METHOD 6 -
;- do.call(cbind, lapply(names.used, function(x){
>>> unlist(by(frame, frame$group, function(y) y[,x] /
>>> mean(y[,x],na.rm=T)))
>>> }))
>>> })
>>> # Took me 1.25-1.32 min
>>> ###################
>>>
>>> #
#
>> frame<-ORIGframe
>> library(plyr)
>> function3 <- function(x) x / mean(x, na.rm = TRUE)
>> system.time({
>> grouping.factor<-"group"
>> myvariables<-names(frame)[2:8]
>> frame3<-ddply(frame, grouping.factor, colwise(fun
lt;-ORIGframe
>> >> system.time({
>> >> frame <- do.call(cbind, lapply(names.used, function(x){
>> >> unlist(by(frame, frame$group, function(y) y[,x] /
>> >> mean(y[,x],na.rm=T)))
>> >> }))
>> >> })
&
##
> >>
> >> ### METHOD 6 - the slowest; using "plyr" and
> >> "ddply":##
> >> frame<-ORIGframe
> >> library(plyr)
> >> function3 <- function(x) x / mean(x, na.rm = TRUE)
> >
Gframe
>> library(plyr)
>> function3 <- function(x) x / mean(x, na.rm = TRUE)
>> system.time({
>> grouping.factor<-"group"
>> myvariables<-names(frame)[2:8]
>> frame3<-ddply(frame, grouping.factor, colwise(function3, myvariables))
>> })
>>
nction(frame) {
>> for(i in 2:ncol(frame)) {
>> frame[,i] <- ave(frame[,i], frame[,1],
>> FUN=function(x)x/mean(x,na.rm=TRUE))
>> }
>> frame
>> }
>>
>> Note that this returns a data.frame and retains the
>> grouping column (the first)
are
> wdunlap tibco.com
>
>> -----Original Message-----
>> From: r-help-boun...@r-project.org
>> [mailto:r-help-boun...@r-project.org] On Behalf Of Bert Gunter
>> Sent: Tuesday, March 30, 2010 10:52 AM
>> To: 'Dimitri Liakhovitski'; 'r-help'
rch 30, 2010 10:52 AM
> To: 'Dimitri Liakhovitski'; 'r-help'
> Subject: Re: [R] Code is too slow: mean-centering variables
> in a data framebysubgroup
>
> ?scale
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
>
>
> -Origina
10 matches
Mail list logo