How to reorder rows with uncommon date order

I have a dataset from 1-Jan, how to reorder it so that 1-Feb is row 1, 2-Feb is row 2, etc. Jan-31 is row 365? I want to reorder the rows this way, so that I can plot the x-axis in this order. And maybe add another column day_order, which is from 1 to 365. Thanks for your help.

I cannot upload the sample data, so that the first few lines are like this:
DF
date value
Jan-1 0.1
Jan-2 0.5
Jan-3 1.2
Jan-4 3.4
...
Dec-31 1.9

The details might depend on the actual structure of your data, including whether it's already sorted by date as implied in your question, whether a date can appear more than once, and whether it's a leap year, but here's a contrived example:

library(tidyverse)

# Fake data
date = seq(as.Date("2018-01-01"), as.Date("2018-12-31"), "1 day") %>% 
         format("%b-%d")

set.seed(2)
dat = tibble(date, value=rnorm(365))

# Ordering
dat = dat %>% 
  mutate(order=c((365 - 30):365, 1:(365 - 31)),
         day.order=1:365) %>% 
  arrange(order) %>% 
  mutate(date = factor(date, levels=unique(date))) %>% 
  select(-order) %>% 
  arrange(date)

dat
   date    value day.order
   <fct>   <dbl>     <int>
 1 Feb-01  0.319        32
 2 Feb-02  1.08         33
 3 Feb-03 -0.284        34
 4 Feb-04 -0.777        35
 5 Feb-05 -0.596        36
 6 Feb-06 -1.73         37
 7 Feb-07 -0.903        38
 8 Feb-08 -0.559        39
 9 Feb-09 -0.247        40
10 Feb-10 -0.384        41

Why not simply slice?

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

set.seed(seed = 32982)

dataset <- tibble(date = format(x = seq.Date(from = as.Date(x = "2019/01/01"),
                                             to = as.Date(x = "2019/12/31"),
                                             length.out = 365),
                                format = "%b-%d"),
                  value = rnorm(n = 365))

dataset %>%
  mutate(day_order = 1:365) %>%
  slice((31 + 1):365, 1:31)
#> # A tibble: 365 x 3
#>    date     value day_order
#>    <chr>    <dbl>     <int>
#>  1 Feb-01  1.51          32
#>  2 Feb-02 -1.27          33
#>  3 Feb-03  1.41          34
#>  4 Feb-04 -0.279         35
#>  5 Feb-05  1.13          36
#>  6 Feb-06 -0.572         37
#>  7 Feb-07 -0.0690        38
#>  8 Feb-08  1.91          39
#>  9 Feb-09 -0.527         40
#> 10 Feb-10  1.25          41
#> # ... with 355 more rows

Created on 2019-06-13 by the reprex package (v0.3.0)

1 Like

Thanks for your responses.
Actually, I meant Feb-1 has the day_order of 1, Feb-2 has the day_order of 2, etc. So Jan-31 in the next year becomes the day_order of 365. There are just 365 days, but the 1st day starts from Feb-2, so it spans two years but just uses part of each year. How to redo the orders?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.