average using across function


I have a dataset with 4 columns, of 2 months Jan and Feb. The data is on an hourly scale.
I am trying to get the final data with an aggregate hourly mean. Only 24 points (average hourly from Jan to Feb).

For that, I was using,

data %>% 
  mutate(hour = lubridate::hour(date)) %>%
  group_by(hour) %>% 
  summarize(across(where(is.numeric), ~ mean(.x, na.rm = TRUE)))

This was working well till yesterday. But, now, when I am applying the same code to the same data. I am getting some errors

Error in `across()`:
! Must be used inside dplyr verbs.
Run `rlang::last_error()` to see where the error occurred.

Could you please tell me what the reason is for that?


I think I should use

  dplyr::summarise((across(where(is.numeric), ~ mean(.x, na.rm = TRUE)))) %>%

You should get in the habit of avoiding using data as the name of a data frame or other subsetable object. You can often get away with it before finding that somehow yours is being treated as if it were a built-in function ‐same goes for df even though it’s widespread in examples everywhere and a host of others. Use Data, dat, df_ instead.

After the quoted, step away from dplyr and use

|> rowMeans(x = _,  na.rm = True)
1 Like

This topic was automatically closed 21 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.