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') [[alternative HTML version deleted]]
eric lee wrote:> 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. >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 > > 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') > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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
Maybe Matching Threads
- transform.data.frame() ignores unnamed arguments when no named argument is provided
- How to read XML in UTF-8 format?
- How to arrange the data
- Accesing data frame members from within functions
- cbind in aggregate formula - based on an existing object (vector)