Try
tempFun <- function(x) sum(!is.na(x))
nonZeros <-
aggregate(pollution["pol"],format(pollution["date"],"%Y-%j"),
FUN = tempFun)
--- On Wed, 12/8/09, Tim Chatterton <tim.chatterton at uwe.ac.uk> wrote:
> From: Tim Chatterton <tim.chatterton at uwe.ac.uk>
> Subject: [R] Counting the number of non-NA values per day
> To: r-help at r-project.org
> Received: Wednesday, 12 August, 2009, 3:26 AM
> I have a long dataframe ("pollution")
> that contains a column of hourly date information ("date")
> and? a column of? pollution? measurements
> ("pol")
>
> I have been happily calculating daily means and daily
> maximums using the aggregate function
>
> DMEANpollution =? aggregate(pollution["pol"],
> format(pollution["date"],"%Y-%j"), mean, na.rm = TRUE)
> DMAXpollution =? aggregate(pollution["pol"],
> format(pollution["date"],"%Y-%j"), max, na.rm = TRUE)
>
> However, I also need to count the number of valid
> measurements for each day to check that the mean and max are
> likely to be valid (for example I need at least 18 hourly
> measurements to calculate a valid daily mean)
>
> Try as I might I have not found a simple way of doing
> this.
> Can anybody help please?
>
> Many thanks,
> Tim.
>
> --
>
> __________________________________________
>
> Dr Tim Chatterton
> Senior Research Fellow
> Air Quality Management Resource Centre
> Faculty of Environment and Technology
> University of the West of England
> Frenchay Campus
> Bristol
> BS16 1QY
>
> Tel: 0117 328 2929
> Fax: 0117 328 3360
> Email: Tim.Chatterton at uwe.ac.uk
>
> ______________________________________________
> 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.
>