Werner W.
2010-Oct-01 11:05 UTC
[R] function which can apply a function by a grouping variable and also hand over an additional variable, e.g. a weight
Hi, I was wondering if there is an easy way to accomplish the following in R: Often I want to apply a function, e.g. weighted.quantile from the Hmisc package to grouped subsets of a data.frame (grouping variable) but then I also need to hand over the weights which seems not possible with summaryBy or aggregate or the like. Is there a function to do this? Currently I do this with loops but it is very slow. I would be very grateful for any hints. Thanks, Werner
Hadley Wickham
2010-Oct-01 12:49 UTC
[R] function which can apply a function by a grouping variable and also hand over an additional variable, e.g. a weight
You might want to check out the plyr package. Hadley On Fri, Oct 1, 2010 at 6:05 AM, Werner W. <pensterfuzzer at yahoo.de> wrote:> Hi, > > I was wondering if there is an easy way to accomplish the following in R: > Often I want to apply a function, e.g. weighted.quantile from the Hmisc package > to grouped subsets of a data.frame (grouping variable) but then I also need to > hand over the weights which seems not possible with summaryBy or aggregate or > the like. > > Is there a function to do this? Currently I do this with loops but it is very > slow. > > I would be very grateful for any hints. > > Thanks, > ?Werner > > > > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code. >-- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
Ozan Bakis
2010-Oct-02 05:11 UTC
[R] function which can apply a function by a grouping variable and also hand over an additional variable, e.g. a weight
Hi,
I was wondering if there is an easy way to accomplish the following in R:
Often I want to apply a function, e.g. weighted.quantile from the Hmisc package
to grouped subsets of a data.frame (grouping variable) but then I also need to
hand over the weights which seems not possible with summaryBy or aggregate or
the like.
Is there a function to do this? Currently I do this with loops but it is very
slow.
I would be very grateful for any hints.
Thanks,
Werner
Hi Werner,
you do not provide a reproducible example, but i guess the following may
give an idea.
### create a toy data frame
set.seed(1)
DF=data.frame(var = rnorm(30,2), factor = sample(30, replace = F),
year = sample(10:12, 30, replace = T))
DF
### define a function that uses wwtd.quantile() function in Hmisc package
### to create a new variable based on weighted quantiles...
myquant=function(x){
require(Hmisc)
y=wtd.quantile(x[,1], w = x[,2], probs = c(0.0,0.5,1))
labs=c("p50","p100")
z=cut(x[,1],breaks=as.numeric(y), include.lowest = T, labels = labs)
x$pc=z
return(x)
}
### split the data by variable year, i.e. grouping variable
### note that factor should be the second variable when splitting!!!
s1=split(DF[,c("var","factor","year")],
DF[,c("year")])
s1
### aplly myquant function to each subset by lapply()
s2=lapply(s1,myquant)
s2
### get the new data frame by unsplit() using year...
DF2=unsplit(s2,DF[,c("year")])
DF2
ozan
[[alternative HTML version deleted]]