I have a portion of my script that was running fine before, but recently has been producing an odd statement after which many of my other functions do not work properly. I a
For consistency sake the grouping variables should be always present when defined earlier and thus are added when select(value)
is executed. ungroup
should resolve it:
qu25 <- mydata %>%
group_by(month, day, station_number) %>%
arrange(desc(value)) %>%
slice(2) %>%
ungroup() %>%
select(value)
The requested result is without warnings:
> mydata %>%
+ group_by(month, day, station_number) %>%
+ arrange(desc(value)) %>%
+ slice(2) %>%
+ ungroup() %>%
+ select(value)
# A tibble: 1 x 1
value
<dbl>
1 113
Did you update dplyr recently by chance? I wonder if your dplyr::arrange call has been adversely effected by https://blog.rstudio.org/2016/06/27/dplyr-0-5-0/
Breaking changes arrange() once again ignores grouping, reverting back to the behaviour of dplyr 0.3 and earlier. This makes arrange() inconsistent with other dplyr verbs, but I think this behaviour is generally more useful. Regardless, it’s not going to change again, as more changes will just cause more confusion.
You can also convert your tibble to a data frame before your select statement using data.frame(). Then dplyr loses track of your grouping variables and isn't worried about them anymore.
qu25 <- mydata %>%
group_by(month, day, station_number) %>%
arrange(desc(value)) %>%
slice(3) %>%
data.frame() %>%
select(value)