[R] aggregate, by, *apply
Abhijit Dasgupta, PhD
adasgupta at araastat.com
Thu Sep 16 01:22:28 CEST 2010
I would approach this slightly differently. I would make func a
function of x and y.
func <- function(x,y){
m <- median(x)
return(m > 2 & m < y)
}
Now generate tmp just as you have. then:
require(plyr)
res <- daply(tmp, .(z), summarise, res=func(x,y))
I believe this does the trick
Abhijit
On 9/15/10 5:45 PM, Mark Ebbert wrote:
> Dear R gurus,
>
> I regularly come across a situation where I would like to apply a function to a subset of data in a dataframe, but I have not found an R function to facilitate exactly what I need. More specifically, I'd like my function to have a context of where the data it's analyzing came from. Here is an example:
>
> ### BEGIN ###
> func<-function(x){
> m<-median(x$x)
> if(m> 2& m< x$y){
> return(T)
> }
> return(F)
> }
>
> tmp<-data.frame(x=1:10,y=c(rep(34,3),rep(35,3),rep(34,4)),z=c(rep("a",3),rep("b",3),rep("c",4)))
> res<-aggregate(tmp,list(z),func)
> ### END ###
>
> The values in the example are trivial, but the problem is that only one column is passed to my function at a time, so I can't determine how 'm' relates to 'x$y'. Any tips/guidance is appreciated.
>
> Mark T. W. Ebbert
> ______________________________________________
> 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.
--
Abhijit Dasgupta, PhD
Director and Principal Statistician
ARAASTAT
Ph: 301.385.3067
E: adasgupta at araastat.com
W: http://www.araastat.com
More information about the R-help
mailing list