0

Given the following dataframe generated by the code:

> df = data.frame(day=c('Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'),
+                 value=c(2,4,1,2,3,4,1))
> df
    day value
1   Mon     2
2  Tues     4
3   Wed     1
4 Thurs     2
5   Fri     3
6   Sat     4
7   Sun     1

I want to convert Mon, ... , Sun to 1, ...7 respectively to reflect the indexing of the day of the week.

Is there an easier/more straightforward way of doing this instead of using a defined function that checks the column value and converts the prefix to its assigned value?

Stoner
  • 846
  • 1
  • 10
  • 30
  • possible duplicate of: https://stackoverflow.com/questions/33008615/converting-day-of-week-to-number-in-r – Ben Aug 20 '19 at 02:37

1 Answers1

1

Define the days in the order you want and then use match

wdays <- c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun")
match(df$day, wdays)
#[1] 1 2 3 4 5 6 7
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213