1

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)] 
zx8754
  • 52,746
  • 12
  • 114
  • 209
Joe
  • 795
  • 1
  • 11
  • Related post: https://stackoverflow.com/questions/16193549/how-can-i-create-a-vector-containing-the-days-of-the-week – zx8754 Jul 20 '23 at 09:35
  • 1
    Thing is im not trying to order them in a standard way, I'm trying to order them based on friday being the first as thats the start of the consecutive days – Joe Jul 20 '23 at 09:39
  • OK, I think I understand the issue, see my answer. – zx8754 Jul 20 '23 at 10:12

2 Answers2

2

Update: We could create a vector of weekdays and use it as factor levels:

# your character string vector
mydays <- c("Friday","Monday","Saturday","Sunday" )

# transfrom to factor
mydays <- factor(mydays, levels = weekdays(ISOdate(1, 1, 1:7)))

# order the factor
sort(mydays)
# [1] Monday   Friday   Saturday Sunday  
# Levels: Monday Tuesday Wednesday Thursday Friday Saturday Sunday

First answer: First convert to factor then order:

# your character string vector
mydays <- c("Friday","Monday","Saturday","Sunday" )

# transfrom to factor
mydays <- factor(mydays, levels = c("Friday", "Saturday", "Sunday", "Monday"))

# order the factor
sort(mydays)

output:

[1] Friday   Saturday Sunday   Monday  
Levels: Friday Saturday Sunday Monday
zx8754
  • 52,746
  • 12
  • 114
  • 209
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Sorry I didn't make it very clear but the days I have won't always be this set of days, they will randomly change from week to week. Although this can be helpful if anyone just needs to do it once with a single set of days – Joe Jul 20 '23 at 09:27
  • No problem. Please see my update. This should work. – TarJae Jul 20 '23 at 09:32
  • 1
    Hope you don't mind the edit. Didn't want to post 99% same answer. – zx8754 Jul 20 '23 at 09:35
  • 2
    Think this will still be a good answer for most people so very helpful. Although in my case i'm trying to order a bit differently where friday is considered first as its the first of the consecutive days. In short this is so i can work out the date of the days. So i know friday is the starting day which has a date of 2023-07-14 then i want the other days to be assigned the 15th, 16th and 17th. Sorry, hopefully that reasoning makes my madness a bit more sensible – Joe Jul 20 '23 at 09:43
2

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
zx8754
  • 52,746
  • 12
  • 114
  • 209