0

I am new to R

I've been struggling to understand how to get these columns for plotting. This columns are from different data. What I need to do is either use The wday() function in the lubridate package as it says that it be useful. Then I need to pivot the data to long format to get the Direction column. The Timeperiod column comes from the lockdown_dates data. Summarise the data to get appropriate averages.

Column      Description
Weekday     Day of week (Sunday through Saturday).
Hour        Hour of day (0 through 23).
Direction   To City or From City
Timeperiod  Before times, Lockdown
Count       Average (mean) number of cyclists per direction per hour for each time period and weekday.

I have these sample tables .


    Date         Timeperiod
1   2020-02-01   Before times
2   2020-02-02   Before times
3   2020-02-03   Before times

    Date        Hour From City   To City
1   2020-02-01   0    0           1
2   2020-02-01   1    0           0
3   2020-02-01   2    0           0

I don't know how to start my code, I was thinking of grouping these to form the data but i know it wont work. I would appreciate if someone can give me an example how to do it.

but I tried this but it only gave me "Friday" not a column.

weekdays(as.Date("4/6/2018 20:14", "%m/%d/%Y"))
  • What is your expected output for the example shared? – Ronak Shah Aug 27 '20 at 03:18
  • I just need to have Weekday, hour, direction,and timeperiod in one data frame so i could plot it. –  Aug 27 '20 at 03:32
  • Hi did my below answer worked for you? If it did, could you accept the answer so that the question can be marked as solved? Thank you. – Ronak Shah Sep 24 '20 at 01:04

1 Answers1

0

You can join the two dataframes and calculate the weekDays for Date.

result <- transform(merge(df1, df2, by = 'Date'), wday = weekdays(Date))

Using dplyr :

library(dplyr)
result <- inner_join(df1, df2, by = 'Date') %>% mutate(wday = weekdays(Date))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213