Skip to content

using complete.cases() with nested factors

4 messages · Andrew Barr, Ken Knoblauch, Hadley Wickham +1 more

#
Andrew Barr <wabarr <at> gmail.com> writes:
that looks like the sample
precipitation data from several
I need to extract years that have
values, while excluding that
I can't figure out how to do this
Any help will be appreciate,
There are likely more elegant solutions but this seems to work.
If the data frame is in a variable named dd

lapply(unique(dd$year), function(x) {s <- subset(dd, year == x)
  if (nrow(s) == 12) s})
#
On Thu, Sep 4, 2008 at 4:19 PM, Ken Knoblauch <ken.knoblauch at inserm.fr> wrote:
I think this is slightly more elegant, and follows the
split-apply-combine strategy:

years <- split(dd, dd$year)
full_years <- Filter(function(df) nrow(df) == 12, years)
do.call("cbind", full_years)

Hadley
#
See ?subset and ?ave and try this:

subset(DF, ave(year, year, FUN = length) == 12)
On Thu, Sep 4, 2008 at 5:04 PM, Andrew Barr <wabarr at gmail.com> wrote: