[R] # values used in a function in tapply

jim holtman jholtman at gmail.com
Sat Nov 15 00:54:16 CET 2008

Try something like this:

amn <- tapply(a[,i], a[, 1], colSums, na.rm=TRUE))

but since the result of the tapply is a vector, you can also use:

amn <- tapply(a[,i], a[, 1],sum, na.rm=TRUE))

On Fri, Nov 14, 2008 at 6:47 PM, Stephanie Shaw <slshaw23 at gmail.com> wrote:
> Hello,
> I am using tapply to pull out data by the day of week and then perform
> functions (e.g. mean). I would like to have the number of values used for
> the calcuation for the functions, sorted by each day of week.  A number of
> entries in any given column are NAs.
> I have tried the following code and simple variants with no luck.
> for (i in 1:length(a[1,])){
>  x<-tapply(a[,i],a[,1],mean, na.rm=TRUE)
>  amn<-tapply(a[,i],a[,1],colSums(!is.na),na.rm=T)}
> Adding an explicit argument to colSums(!is.na) {e.g. colSums(!is.na(a[,i]))}
> produced an error that said colSums was not a function. Trying an
> imbedded tapply instead of the colSums didn't work either. Using the sum
> function added the values, not the count of the values.
> Thank you for any suggestions,
> Stephanie
>        [[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.

Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

More information about the R-help mailing list