Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in aggregate.data.frame : arguments must have same length

Tags:

r

aggregate

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
like image 811
RHatesMe Avatar asked Feb 04 '15 16:02

RHatesMe


People also ask

What does arguments must have the same length?

frame(as. data. frame(x), …) : arguments must have same length”. The reason for this error message is that we have not specified the grouping column properly within the aggregate function.

How do you aggregate data in R?

In order to use the aggregate function for mean in R, you will need to specify the numerical variable on the first argument, the categorical (as a list) on the second and the function to be applied (in this case mean ) on the third. An alternative is to specify a formula of the form: numerical ~ categorical .


2 Answers

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.

like image 126
user3019973 Avatar answered Oct 12 '22 16:10

user3019973


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) 
like image 28
jimjamslam Avatar answered Oct 12 '22 15:10

jimjamslam