Home > Software engineering >  Sorting date format column as month.abb order in R
Sorting date format column as month.abb order in R

Time:05-25

I have a column which is in date format. However the months are not arrange in month.abb normal sequence (e.g, Jan - Dec). I tried it but could not find a way that work for me.

Any way I can do it please?

Refer the code below

> # create layernames and date tibble 
> names_dates <- tibble(names = names(idw2),
                        date = time(idw2))  %>% 
    select(date)
> head(names_dates, 20)
# A tibble: 20 x 1
   date      
   <date>    
 1 1998-04-01
 2 1999-04-01
 3 2000-04-01
 4 2001-04-01
 5 2002-04-01
 6 2003-04-01
 7 2004-04-01
 8 2005-04-01
 9 2006-04-01
10 2007-04-01
11 2008-04-01
12 2009-04-01
13 2010-04-01
14 2011-04-01
15 2012-04-01
16 2013-04-01
17 2014-04-01
18 2015-04-01
19 2016-04-01
20 2017-04-01

dates_names_ordered <-

here is the dput of my data

> dput(names_dates)
structure(list(date = structure(c(10317, 10682, 11048, 11413, 
11778, 12143, 12509, 12874, 13239, 13604, 13970, 14335, 14700, 
15065, 15431, 15796, 16161, 16526, 16892, 17257, 10439, 10804, 
11170, 11535, 11900, 12265, 12631, 12996, 13361, 13726, 14092, 
14457, 14822, 15187, 15553, 15918, 16283, 16648, 17014, 17379, 
10561, 10926, 11292, 11657, 12022, 12387, 12753, 13118, 13483, 
13848, 14214, 14579, 14944, 15309, 15675, 16040, 16405, 16770, 
17136, 17501, 10258, 10623, 10988, 11354, 11719, 12084, 12449, 
12815, 13180, 13545, 13910, 14276, 14641, 15006, 15371, 15737, 
16102, 16467, 16832, 17198, 10227, 10592, 10957, 11323, 11688, 
12053, 12418, 12784, 13149, 13514, 13879, 14245, 14610, 14975, 
15340, 15706, 16071, 16436, 16801, 17167, 10408, 10773, 11139, 
11504, 11869, 12234, 12600, 12965, 13330, 13695, 14061, 14426, 
14791, 15156, 15522, 15887, 16252, 16617, 16983, 17348, 10378, 
10743, 11109, 11474, 11839, 12204, 12570, 12935, 13300, 13665, 
14031, 14396, 14761, 15126, 15492, 15857, 16222, 16587, 16953, 
17318, 10286, 10651, 11017, 11382, 11747, 12112, 12478, 12843, 
13208, 13573, 13939, 14304, 14669, 15034, 15400, 15765, 16130, 
16495, 16861, 17226, 10347, 10712, 11078, 11443, 11808, 12173, 
12539, 12904, 13269, 13634, 14000, 14365, 14730, 15095, 15461, 
15826, 16191, 16556, 16922, 17287, 10531, 10896, 11262, 11627, 
11992, 12357, 12723, 13088, 13453, 13818, 14184, 14549, 14914, 
15279, 15645, 16010, 16375, 16740, 17106, 17471, 10500, 10865, 
11231, 11596, 11961, 12326, 12692, 13057, 13422, 13787, 14153, 
14518, 14883, 15248, 15614, 15979, 16344, 16709, 17075, 17440, 
10470, 10835, 11201, 11566, 11931, 12296, 12662, 13027, 13392, 
13757, 14123, 14488, 14853, 15218, 15584, 15949, 16314, 16679, 
17045, 17410), class = "Date")), row.names = c(NA, -240L), class = c("tbl_df", 
"tbl", "data.frame"))

CodePudding user response:

A possible solution, using lubridate:

library(dplyr)
library(lubridate)


df %>% 
  arrange(ymd(date) %>% year, ymd(date) %>% month)

#> # A tibble: 240 x 1
#>    date      
#>    <date>    
#>  1 1998-01-01
#>  2 1998-02-01
#>  3 1998-03-01
#>  4 1998-04-01
#>  5 1998-05-01
#>  6 1998-06-01
#>  7 1998-07-01
#>  8 1998-08-01
#>  9 1998-09-01
#> 10 1998-10-01
#> # ... with 230 more rows
  •  Tags:  
  • r
  • Related