Skip to content

using tapply on a data frame in a function

3 messages · eric lee, Peter Dalgaard, Dieter Menne

#
eric lee wrote:
This is neither caused by tapply, data frames, nor functions....

You are trying to use a character string ('value1') as if it were a 
variable name. It wouldn't work either to say tapply('value1', cluster, 
mean) would it?

You might be looking for tapply(framename[[index]],....), or 
with(framename, tapply(get(index), ....)),
or maybe some concoction involving substitute().

  
    
#
eric lee <ericlee100 <at> gmail.com> writes:
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