Error in aggregate.data.frame: arguments must be the same length

I keep getting this error, and I'm not quite sure what that means. All my variable names are consistent and there are no typos. Did I miss something?

Code

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

Produces an error

  Error in aggregate.data.frame(datNew, by = list("x", "y", : arguments must have same length 
+6
source share
5 answers

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

Some functions, such as subset , allow you to directly specify the column names of the object they are working on. The aggregate function does not, so any dataNew columns listed in the by argument should be specifically mentioned as such. Try the following:

 datNewagg <- aggregate(dataNew, by = list( x = dataNew$x, y = dataNew$y, z = dataNew$z, a = dataNew$a, ab = dataNew$ab), FUN = mean) 
+7
source

I used this error.
A simple solution to remove this error is to write all the variables along with their data set name, for example, "ds_name $ var_name".
I'm not sure if the name of your dataset is your business, so I will give you another example.

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

Here, “hr” is the name of the data set and “Year.Total”, “Hospital_ID”, “District_ID”, “Instrument_ID” are the variables in the data set “hr”.

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

+4
source

Check out the class(dataNew) . If it is not data.frame, this dataNew <- data.frame(dataNew) should resolve the error before aggregation or

 datNewagg <- aggregate (data.frame(dataNew), by = list('x', 'y', 'z', 'a', 'ab'), FUN = mean) 
+1
source

Using data.frame as a by argument works for me, try the following:

 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 data.frame with columns as these arguments

0
source

When you use with(...,aggregate(...)) , do not put the column names in quotation marks.

0
source

Source: https://habr.com/ru/post/981992/


All Articles