“Adding missing grouping variables” message in dplyr in R

后端 未结 3 1469
时光说笑
时光说笑 2020-11-30 05:48

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

相关标签:
3条回答
  • 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
    
    0 讨论(0)
  • 2020-11-30 06:05

    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.

    0 讨论(0)
  • 2020-11-30 06:20

    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)
    
    0 讨论(0)
提交回复
热议问题