rtidyverselubridateweekdayweekend

Sum up ride_length for weekdays vs weekends and compare for casual and annual members


Hi Im a beginner in R so dont know much functionality to go about to perform this operation even though in my head i know what to do just dont know how to do it.

So I have data for of ride length I want to sum up for weekdays vs weekends and compare it with annual and casual members. I have used the wday() to convert the dates to '1' to '7'. Now i want to filter out '2' to '6' (weekdays) and sum the ride_lenth and filter out '1' & '7' (weekends) and sum that ride_length and then use the aggregate() to compare them with the casual and annual members usage. That is what i have decided.

member_type  ride_length     date    month     day       year      day_of_week      weekday_num

casual       5280        2020-07-01   Jul        01       2020         Wednesday              4
casual       9840        2020-07-01   Jul        01       2020         Wednesday              4

Any other path to this would be welcome too.


Solution

  • unfortunately I can not test the code due to missing input and desired output. But you should be able to make these lines work for you:

    library(dplyr)
    
    # your data.frame/tibble
    df %>%
        # create variable to indicate weekend or not (check the weekend day names)
        dplyr::mutate(day_type = ifelse(day_of_week %in% c("Saturday", "Sunday"), "WEEKEND","WEEK")) %>%
        # build gouping by member type and day type
        dplyr::group_by(member_type, day_type) %>%
        # summarise total ride length
        dplyr::summarize(total_ride_length = sum(ride_length, na.rm = TRUE))
    

    Just as an advice: possibly there are some holidays you should consider, as they can be on a working day but show the behaviour of a weekend day (due to most people having free time to rent and ride bikes or viceverse if people predominantly rent to get to and from work)