12

I keep getting this error and I'm not quite sure what it means. All of my variable names are consistent and there are no typos. Am I missing something here?

The code

datNewagg <- aggregate (dataNew, by = list('x', 'y', 'z', 'a', 'ab'), 
                                                             FUN = mean)  

Produces the error

  Error in aggregate.data.frame(datNew, by = list("x", "y",  : 
  arguments must have same length
user20650
  • 24,654
  • 5
  • 56
  • 91
RHatesMe
  • 143
  • 1
  • 2
  • 8
  • 1
    Can you dput the exact datafame? – user2600629 Feb 04 '15 at 16:06
  • Can you show the `aggregate` call, as it mat be incorrectly specified. ty – user20650 Feb 04 '15 at 16:13
  • 4
    You get a similar error with `aggregate(mtcars, list("am"), mean)`. But if you specify it like `aggregate(mtcars, list(mtcars$am), mean)` or `aggregate(. ~ am , mean, data=mtcars)` everything is okay – user20650 Feb 04 '15 at 16:21
  • `datNewagg <- aggregate (dataNew, by = list('x', 'y', 'z', 'a', 'ab'), FUN = mean)` – RHatesMe Feb 04 '15 at 16:30
  • ok, have a look at the comment above on specifying aggregate function. . Try `datNewagg <- aggregate (. ~ x + y + z + a + ab), FUN = mean, data=dataNew)` or `datNewagg <- with(dataNew, aggregate (dataNew, by = list(x, y, z, a, ab), FUN = mean))` – user20650 Feb 04 '15 at 16:34

5 Answers5

15

Assuming it's not a typo (the data frame is called dataNew in your call but datNew in the error), are x, y, z, a and ab the names of columns in dataNew?

Some functions, like subset, will allow you to specify column names of the object they're working on directly. The aggregate function doesn't, so any columns of dataNew listed in the by argument need to specifically referred to as such. Try this:

datNewagg <- aggregate(dataNew,
    by = list(
        x = dataNew$x,
        y = dataNew$y,
        z = dataNew$z,
        a = dataNew$a,
        ab = dataNew$ab),
    FUN = mean) 
jimjamslam
  • 1,988
  • 1
  • 18
  • 32
12

I used get this error.
The simple solution to remove this error is to write all the variables along with their dataset name like "ds_name$var_name".
I'm not sure what is the dataset name is your case, so I'll give you another similar example.

curYearRev <-aggregate(hr$Year.Total, by = list(hr$Hospital_ID,hr$District_ID,hr$Instrument_ID) , FUN = sum)

Here, "hr" is the dataset name and "Year.Total", "Hospital_ID", "District_ID", "Instrument_ID" are the variables in "hr" dataset.

Writing aggregate functions in this way will never give you any errors again.

user3019973
  • 121
  • 1
  • 3
1

Check class(dataNew). If it's not a data.frame, this dataNew <- data.frame(dataNew) before aggregation should solve the error or

datNewagg <- aggregate (data.frame(dataNew), by = list('x', 'y', 'z', 'a', 'ab'), 
                                                         FUN = mean)
vital_dml
  • 1,176
  • 7
  • 7
1

When you use with(...,aggregate(...)) don't put your column names in quotes.

Dmitriy
  • 5,525
  • 12
  • 25
  • 38
ginn
  • 101
  • 8
0

Using data.frame as by argument works for me try this:

datNewagg <- aggregate (dataNew, by = dataNew[c('x', 'y', 'z', 'a', 'ab')],                                                             FUN = mean)

I mean don't give the by argument, just the name of the arguments, give a data.frame with columns as these arguments

rar
  • 894
  • 1
  • 9
  • 24
Pavel Shliaha
  • 773
  • 5
  • 16