1

I have quite many variables to feed like to aggregate and apparently I'm lazy to explicitly type all especially they change once in a while but those I'd like to exclude are mostly the same. I tried to write

hru.mean <-
    aggregate(.-LULC~GIS, hru.annual, "mean")

but apparently it is not quite right as it says

Error in aggregate.data.frame(mf[1L], mf[-1L], FUN = FUN, ...) : 
  no rows to aggregate
In addition: Warning message:
In Ops.factor(., LULC) : - not meaningful for factors

I just saw this, so it looks like it works for lm but does not work for aggregate. Are there any alternatives?

Community
  • 1
  • 1
mlt
  • 1,595
  • 2
  • 21
  • 52

2 Answers2

3

Why not just pass a subset as the data

aggregate(. ~ Species, data = iris, mean)

compared with

aggregate(. ~ Species, iris[,-which(names(iris) == 'Sepal.Length')], mean)
mnel
  • 113,303
  • 27
  • 265
  • 254
  • Subsetting will definitely work. I was not sure whether I was doing something wrong with formula. I guess there could be a better support in aggregate() for that. – mlt Oct 19 '12 at 17:20
3

Something like this where you limit the columns in the data set instead:

aggregate(.~cyl, mtcars[, !colnames(mtcars) %in% c("hp")], mean)
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • I realized that I can also feed something like `subset(hru.annual, select=-c(LULC, HRU))`. Perhaps it is a separate question but nevertheless... I have a gut feeling that `subset` generally is neater. Though I don't know internals. – mlt Oct 19 '12 at 17:25