dplyr - arrange () according to two criteria per group

后端 未结 3 1027
名媛妹妹
名媛妹妹 2021-01-28 05:50

I have hourly weather collected for hundreds of farms for a period of five weeks before a sampling event. I want to determine the average Air_Temp for the three weeks prior to t

相关标签:
3条回答
  • 2021-01-28 05:58

    In addition to my comments you can also do the following :

    sorted <- Weather %>% 
              arrange(Date, Hour) %>%
              group_by(File)
    
    0 讨论(0)
  • 2021-01-28 06:10

    group_by shouldn't be necessary for this, it's typically used for when you are looking to perform some kind of aggregate on your data. The arrange will sort first by the File, then by the Date within each file, then by the Hour within each Date. This should get you the structure you're looking for.

    Weather1 <- Weather%>%
                arrange(File, Date, Hour)
    
    0 讨论(0)
  • 2021-01-28 06:19

    I am using ‘0.5.0.9001’ version of dplyr (pre-release of 0.6.0). The new version will be released soon.

    for grouped df, the arrange will ignore grouping information by default:

    ## S3 method for class 'grouped_df'
    arrange(.data, ..., .by_group = FALSE)
    

    So you would have to manually set .by_group = TRUE in order to tell arrange that the df is grouped:

    Weather1 <- Weather %>%
        group_by(File) %>%
        arrange(Date, Hour, .by_group = TRUE)
    
    0 讨论(0)
提交回复
热议问题