Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204922

R - convert date column in daily format to hourly granularity

$
0
0

I have the following dataframe df:

> head(df)
        date    demand
1 2019-09-21    6
2 2019-09-21    1
3 2019-09-21    9
4 2019-09-21    3
5 2019-09-21    12
6 2019-09-21    3

and I will like it to have hourly granularity as follows:

> head(df)
                 date   demand
1 2019-09-21 00:00:00   6
2 2019-09-21 01:00:00   1
3 2019-09-21 02:00:00   9
4 2019-09-21 03:00:00   3
5 2019-09-21 04:00:00   12
6 2019-09-21 05:00:00   3

Is there a way to do this efficiently in R? The closest answer I've found is R Programming: Create hourly time intervals in an array with for loop and Matching and Replacing values of a column in a dataframe by date in r but I can't seem implement them for my problem.. Some insights will be deeply appreciated!

Remark: There are multiple days in the dataframe, each day contains 24 hours of daates as above in df. The first date should have hour starting at 12am (00:00:00) and the last date has hour at 11pm (23:00:00).


Viewing all articles
Browse latest Browse all 204922

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>