Home > database >  Order time with year-week format
Order time with year-week format

Time:07-11

I want to order year weeknumber format of data

I have a data frame as follow :

df =c(
2022 W12,
.     .
.     .
.     .
2026 W52) %>% data.frame()

I used the following code but it does not arrange it properly

df <- df %>% arrange((date))

the result now shows 2026 w9 for the last row which does not make sense it has to be 2026 w52

CodePudding user response:

You could use gtools::mixedorder() dplyr::slice().

df %>% 
  slice(gtools::mixedorder(date))

#       date
# 1  2022 W9
# 2 2022 W52
# 3  2026 W9
# 4 2026 W52
Data
df <- data.frame(date = c("2022 W52", "2026 W52", "2022 W9", "2026 W9"))

CodePudding user response:

We may gsub the characters away, as.numeric and order.

dat[order(dat$date, as.numeric(gsub('\\D', '', dat$week))), ]
#     date week
# 1   2022  W22
# 5   2022  W23
# 12  2022  W24
# 19  2022  W25
# 26  2022  W26
# 33  2022  W27
# 40  2022  W28
# 47  2022  W29
# 54  2022  W30
# 61  2022  W31
# 68  2022  W32
# 75  2022  W33
# 82  2022  W34
# 89  2022  W35
# 96  2022  W36
# 103 2022  W37
# 110 2022  W38
# 117 2022  W39
# 124 2022  W40
# 131 2022  W41
# 138 2022  W42
# 145 2022  W43
# 152 2022  W44
# 159 2022  W45
# 166 2022  W46
# 173 2022  W47
# 180 2022  W48
# 187 2022  W49
# 194 2022  W50
# 201 2022  W51
# 208 2022  W52
# 215 2023   W1
# 222 2023   W2
# 229 2023   W3
# 236 2023   W4
# 243 2023   W5
# 250 2023   W6
# 257 2023   W7
# 264 2023   W8
# 271 2023   W9
# 278 2023  W10
# 285 2023  W11
# 292 2023  W12
# 299 2023  W13
# 306 2023  W14
# 313 2023  W15
# 320 2023  W16
# 327 2023  W17
# 334 2023  W18
# 341 2023  W19
# 348 2023  W20
# 355 2023  W21
# 362 2023  W22

Data:

dat <- structure(list(date = c("2022", "2023", "2022", "2022", "2022", 
"2022", "2023", "2022", "2023", "2023", "2023", "2022", "2022", 
"2022", "2023", "2023", "2023", "2023", "2023", "2023", "2022", 
"2023", "2022", "2023", "2023", "2022", "2023", "2022", "2022", 
"2022", "2022", "2022", "2022", "2023", "2022", "2022", "2022", 
"2023", "2022", "2023", "2022", "2022", "2022", "2022", "2022", 
"2023", "2022", "2023", "2022", "2022", "2022", "2023", "2023"
), week = c("W30", "W6", "W27", "W49", "W48", "W43", "W9", "W22", 
"W20", "W5", "W2", "W44", "W45", "W28", "W11", "W21", "W13", 
"W4", "W18", "W15", "W29", "W19", "W25", "W22", "W17", "W51", 
"W7", "W47", "W31", "W24", "W50", "W23", "W36", "W3", "W39", 
"W42", "W52", "W1", "W35", "W10", "W33", "W46", "W26", "W34", 
"W40", "W16", "W41", "W12", "W38", "W32", "W37", "W14", "W8")), row.names = c(54L, 
250L, 33L, 187L, 180L, 145L, 271L, 1L, 348L, 243L, 222L, 152L, 
159L, 40L, 285L, 355L, 299L, 236L, 334L, 313L, 47L, 341L, 19L, 
362L, 327L, 201L, 257L, 173L, 61L, 12L, 194L, 5L, 96L, 229L, 
117L, 138L, 208L, 215L, 89L, 278L, 75L, 166L, 26L, 82L, 124L, 
320L, 131L, 292L, 110L, 68L, 103L, 306L, 264L), class = "data.frame")

CodePudding user response:

A possible solution, based on gtools::mixedsort:

library(dplyr)

df =c(
  "2022 W12",
  "2022 W1",
  "2022 W9",
  "2026 W52",
  "2026 W9") %>% data.frame(x = .)

df %>% 
  mutate(x = gtools::mixedsort(x))

#>          x
#> 1  2022 W1
#> 2  2022 W9
#> 3 2022 W12
#> 4  2026 W9
#> 5 2026 W52
  • Related