I have a vector of some days of the week which I need to be in order, I have a feeling there might be a package that deal with days but could well be wrong.
mydays
"Friday" "Monday" "Saturday" "Sunday"
I want these to become. Also worth noting here that this will be a bit different and i cant just normally order by days of the week as Monday will be first (I don't want this), my days will always be consecutive days which might make it a bit easer to order
mydays
"Friday" "Saturday" "Sunday" "Monday"
I've tried buy looks like it doesnt work as theres nothing to order on as R doesn't know any difference between Friday and Saturday for example.
mydays[order(mydays)]
We can rotate the factor levels based on match of the first day of the week in the input vector, in your example, it is Friday:
#function to rotate vector
#https://stackoverflow.com/a/30542172/680068
shifter <- function(x, n = 1) {
if (n == 0) x else c(tail(x, -n), head(x, n))
}
# input
mydays <- c("Friday","Monday","Saturday","Sunday" )
# get weekdays
wd <- weekdays(ISOdate(1, 1, 1:7))
# shift based on starting day
wdShift <- shifter(wd, which(wd == mydays[1]) - 1)
# transfrom to factor
mydays <- factor(mydays, levels = wdShift)
# order the factor
sort(mydays)
# [1] Friday Saturday Sunday Monday
# Levels: Friday Saturday Sunday Monday Tuesday Wednesday Thursday