'How to use group_by without ordering alphabetically?

I'm trying to visualize some bird data, however after grouping by month, the resulting output is out of order from the original data. It is in order for December, January, February, and March in the original, but after manipulating it results in December, February, January, March.

Any ideas how I can fix this or sort the rows?

This is the code:

BirdDataTimeClean <- BirdDataTimes %>% 
  group_by(Date) %>% 
  summarise(Gulls=sum(Gulls), Terns=sum(Terns), Sandpipers=sum(Sandpipers), 
  Plovers=sum(Plovers), Pelicans=sum(Pelicans), Oystercatchers=sum(Oystercatchers), 
  Egrets=sum(Egrets), PeregrineFalcon=sum(Peregrine_Falcon), BlackPhoebe=sum(Black_Phoebe), 
  Raven=sum(Common_Raven))

BirdDataTimeClean2 <- BirdDataTimeClean %>%
  pivot_longer(!Date, names_to = "Species", values_to = "Count")



Solution 1:[1]

You haven't shared any workable data but i face this many times when reading from csv and hence all dates and data are in character. as suggested, please convert the date data to "date" format using lubridate package or base as.Date() and then arrange() in dplyr will work or even group_by

example :toy data created

birds <- data.table(dates = c("2020-Feb-20","2020-Jan-20","2020-Dec-20","2020-Apr-20"),
           species = c('Gulls','Turns','Gulls','Sandpiper'),
           Counts = c(20,30,40,50)

str(birds) will show date is character (and I have not kept order)

using lubridate convert dates

birds$dates%>%lubridate::ymd() will change to date data-type

 birds$dates%>%ymd()%>%str()
 Date[1:4], format: "2020-02-20" "2020-01-20" "2020-12-20" "2020-04-20"

save it with birds$dates <- ymd(birds$dates) or do it in your pipeline as follows

now simply so the dplyr analysis:

    birds%>%group_by(Months= ymd(dates))%>%
  summarise(N=n()
            ,Species_Count = sum(Counts)
            )%>%arrange(Months)

will give

# A tibble: 4 x 3
  Months         N Species_Count
  <date>     <int>         <dbl>
1 2020-01-20     1            30
2 2020-02-20     1            20
3 2020-04-20     1            50

However, if you want Apr , Jan instead of numbers and apply as.Date() with format etc, the dates become "character" again. I woudl suggest you keep your data that way and while representing in output for others -> format it there with as.Date or if using DT or other datatables -> check the output formatting options. That way your original data remains and users see what they want. this will make it character

 birds%>%group_by(Months= as.character.Date(dates))%>%
   summarise(N=n()
             ,Species_Count = sum(Counts)
   )%>%arrange(Months)

A tibble: 4 x 3

Months          N Species_Count
  <chr>       <int>         <dbl>
1 2020-Apr-20     1            50
2 2020-Dec-20     1            40
3 2020-Feb-20     1            20
4 2020-Jan-20     1            30

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman