df<-data.frame(record_id=1:5,group=c('A','B','C','D','E'),
date_start=c('Apr-22','Aug-21','Jan-22','Feb-22','Dec-21'))
This is the code I'm attempting to use to order my column by chronological date vs alphabetically.
arrange(ymd(date_start))
I feel like I've tried reformatting/reclassing in every known way, and it's still either giving an error or not sorting chronologically.
CodePudding user response:
The format should be myd
as it is in the order of month, year, and (day) was not there, so use truncated = 1
library(dplyr)
library(lubridate)
df %>%
arrange(myd(date_start, truncated = 1))
Or can also use my
as there is missing 'day'
df %>%
arrange(my(date_start))
-output
record_id group date_start
1 2 B Aug-21
2 5 E Dec-21
3 3 C Jan-22
4 4 D Feb-22
5 1 A Apr-22
CodePudding user response:
Here is an alternative approach:
library(lubridate)
library(dplyr)
df %>%
arrange(date_start1 = myd(paste0(date_start,"-01")))
record_id group date_start
1 2 B Aug-21
2 5 E Dec-21
3 3 C Jan-22
4 4 D Feb-22
5 1 A Apr-22