Search code examples
rsortingweekday

Sort a dataframe in R on a column containing days of the week


I am using R for data analysis. I have a data frame that contains data on the steps walked per day by a number of users. My data looks something like this -

User_id Week_day Step_count
1 Friday 5000
1 Saturday 6000
1 Sunday 2000
1 Monday 9000
1 Tuesday 6000
1 Wednesday 8000
1 Thursday 8000
2 Friday 1000
2 Saturday 8000
2 Sunday 8000
2 Monday 2000
2 Tuesday 1000
2 Wednesday 2000
2 Thursday 2000
3 Friday 5000
3 Saturday 8000
3 Sunday 8000
3 Monday 5000
3 Tuesday 5000

and so on. Note how the day starts from Friday. Thus it appears to be sorted alphabetically, by day, for each user. I would like to start from Monday for each user. The reason being that when I put this data onto a plot, I would like to start the days from Monday. Please help me achieve this in R. Thank you

I've only tried searching online but haven't found anything to help me do this.


Solution

  • Based on I_O's comment, here is a reproducible example. Currently in your dataset, Week_day is a character, so R is ordering the values alphabetically. Converting Week_day to a factor and specifying the order of the levels will make it so any plots will be in the right order. More information on data types here.

    library(tidyverse)
    
    df <- readr::read_table("
    User_id Week_day    Step_count
    1   Friday  5000
    1   Saturday    6000
    1   Sunday  2000
    1   Monday  9000
    1   Tuesday 6000
    1   Wednesday   8000
    1   Thursday    8000
    2   Friday  1000
    2   Saturday    8000
    2   Sunday  8000
    2   Monday  2000
    2   Tuesday 1000
    2   Wednesday   2000
    2   Thursday    2000
    3   Friday  5000
    3   Saturday    8000
    3   Sunday  8000
    3   Monday  5000")
    
    ## not ordered
    df %>%
      ggplot(aes(x = Week_day, y = Step_count)) + 
      geom_boxplot()
    

    enter image description here

    ## ordered
    df %>%
      mutate(Week_day = factor(Week_day, levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'))) %>% 
      ggplot(aes(x = Week_day, y = Step_count)) + 
      geom_boxplot()
    

    enter image description here