Error in aggregate.formula(

Hi!
I need your help with the following case, since I tried these ways and it gives me those errors.

Option-01:
counts <- aggregate(all_trips_v2$ride_length ~ all_trips_v2$member_casual + all_trips_v2$day_of_week, FUN = mean)

ERROR: Error in aggregate.data.frame(lhs, mf[-1L], FUN = FUN, ...) : no rows to aggregate

Option-02:
counts <- aggregate(formula = ride_length ~ member_casual + day_of_week,
data = all_trips_v2,
FUN = mean)

ERROR: Error in aggregate.formula(formula = ride_length ~ member_casual + day_of_week, :
argument 'x' is missing -- it has been renamed from 'formula'

1 Like

I can't test as you didn't provide a reprex, so this is my best guess.

Option 1: I'm pretty sure in the formula you can't use $ to access columns, you need to use the form from option 2, providing the data in the data argument, and using the column names directly in the formula.

Option 2: note the error message argument 'x' is missing. And indeed, if you look at ?aggregate, you can find its usage:

## S3 method for class 'formula'
aggregate(x, data, FUN, ...,
          subset, na.action = na.omit)

So even if you provide a formula, the argument name is x. You need to call it as:

counts <- aggregate(x = ride_length ~ member_casual + day_of_week,
data = all_trips_v2,
FUN = mean)

or using a positional argument (not specifying the name):

counts <- aggregate(ride_length ~ member_casual + day_of_week,
data = all_trips_v2,
FUN = mean)

The error message explains why your code might have worked in the past: argument 'x' is missing -- it has been renamed from 'formula'. So in older versions of R, this argument was indeed called formula, and your option 2 would have worked. If you're curious, you can find that this change was introduced in R 4.2.0 because the old name was creating a problem.

Wow, thank you very much for your help.
This is driving me crazy.

I tried those last two options as you indicated and it sent me the following error:

Error in aggregate.data.frame(lhs, mf[-1L], FUN = FUN, ...) :
no rows to aggregate

This is the code used:
counts <- aggregate(ride_length ~ member_casual + day_of_week,
data = all_trips_v2,
FUN = mean)

This suggests there could be a problem with your input data, I can't answer without seeing it. Could be a problem with NAs, with factor levels, ...

First, do check the results of:

table(all_trips_v2$member_casual, all_trips_v2$day_of_week, useNA = 'ifany')

and

table(is.na(all_trips_v2$ride_length))

Maybe something will be obvious here. Otherwise, could you share a reprex?

Hi!

This is the results:

table(all_trips_v2$member_casual, all_trips_v2$day_of_week, useNA = 'ifany')

      Sunday  Monday Tuesday Wednesday Thursday  Friday Saturday    <NA>

casual 0 0 0 0 0 0 0 1825218
member 0 0 0 0 0 0 0 3029532
0 0 0 0 0 0 0 868735

table(is.na(all_trips_v2$ride_length))
FALSE TRUE
4854750 868735

Here you have it: the day_of_week column is filled with NA, probably there was a problem in a previous step.

Also note how many of your member_casual entries are actually empty.

Okay, Thanks!
i nedd verify the previous steps...

thank you very much!!!!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.