Home > Blockchain >  Sort by Month Abbreviation and Year in R
Sort by Month Abbreviation and Year in R

Time:03-18

I couldn't find the exact answer to this but if it already exists, please share. I'm using R and I have a column that is the month abbreviation and year so the output would be as follows:

Jan2021
Jan2022
May2021
Apr2022
Aug2021
Jul2022

If I sort this column, I get:

Apr2022
Aug2021
Jan2021
Jan2022
Jul2022
May2021

But I need to sort by actual month/year, for example:

Jan2021
May2021
Aug2021
Jan2022
Apr2022
Jul2022

How do I achieve this?

CodePudding user response:

You'll want to convert your text into a date and sort by that. I like the lubridate package for convenient date manipulation. The dmy function expects to receive data in day-month-year order and does the work to interpret Apr as April without you having to specify the exact format.

df$date = lubridate::dmy(paste(1,df$my_col))
df <- df[order(df$date),]

CodePudding user response:

Without packages using as.Date.

dat[order(as.Date(paste0('1', dat$V1), '%d%b%Y')), ]
#        V1         V2
# 1 Jan2021 -0.4202562
# 3 May2021  0.7101010
# 5 Aug2021  0.6140036
# 2 Jan2022 -0.2714406
# 4 Apr2022 -0.2261735
# 6 Jul2022  0.4990879

Data:

dat <- structure(list(V1 = c("Jan2021", "Jan2022", "May2021", "Apr2022", 
"Aug2021", "Jul2022"), V2 = c(-0.420256183432969, -0.27144061344182, 
0.71010096203704, -0.226173491695943, 0.614003577539037, 0.499087929183462
)), class = "data.frame", row.names = c(NA, -6L))
  •  Tags:  
  • r
  • Related