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
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?