Dear Sephanie,
Try the following:

# Data set including some NA values
set.seed(123)
X=cbind(c(3, 2, 2, 1, 2, 5, 5, 4, 1, 1),matrix(rnorm(100),ncol=10))
colnames(X)=c('day',paste('X',1:10,sep=""))
X[2,2]<-X[5,10]<-X[3,8]<-NA
X

# mean excluding NA values
# in parenthesis are the number of observations we used for the calculations
noquote(apply(X[,-1],2,tapply,X[,1],function(x){
mx=round(mean(x,na.rm=TRUE),4)
used=sum(!is.na(x))
paste(mx,"(",used,")",sep="")
}))


HTH,

Jorge


On Mon, Nov 17, 2008 at 2:51 PM, Stephanie Shaw <[EMAIL PROTECTED]> wrote:

> Thank you very much for the suggestions, however I do not think they will
> help. Please let me know if I misunderstand.
>
> 1) Just using "colSums" without the "(!is.na)" portion provides:
>  Error in FUN(X[[1L]], ...) :
>  'x' must be an array of at least two dimensions
>
>
> 2) In that context, sum appears to add the values, not the number of
> values.
>
> Thank you,
> Stephanie
>
> On Fri, Nov 14, 2008 at 3:54 PM, jim holtman <[EMAIL PROTECTED]> wrote:
>
> > 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 <[EMAIL PROTECTED]>
> > 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@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<
> 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?
> >
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to