Message-ID: <loom.20080928T193603-287@post.gmane.org>
Date: 2008-09-28T19:37:19Z
From: Dieter Menne
Subject: using tapply on a data frame in a function
eric lee <ericlee100 <at> gmail.com> writes:
>
> Hello,
>
> I'm trying to use tapply to find group means in a function. It works
> outside of a function, but I get the error message from the following code:
> "Error in tapply(index, cluster, mean) : arguments must have same length."
> Any suggestions? Thanks.
>
> eric
>
> d <- data.frame(cbind(cluster=1:2, value1=1:10, value2=11:20))
> d
>
> FindClusterTraits <- function(framename, index){
> MeanCluster <- with(framename, tapply(index, cluster, mean))
> return(MeanCluster)
> }
>
> d2 <- FindClusterTraits(d, 'value1')
>
Thanks for the self-running example! Not really a function problem, though.
d <- data.frame(cbind(cluster=1:2, value1=1:10, value2=11:20))
d
FindClusterTraits <- function(framename, index){
tapply(framename[[index]], framename[["cluster"]], mean)
}
FindClusterTraits(d, 'value1')
Note that the return(MeanCluster) is not required in R.
Dieter