Reputation: 4482
I have a data table, dt, that looks like
date value
1: 2016-06-06 0
2: 2016-06-13 0
3: 2016-06-20 0
4: 2016-06-27 0
5: 2016-07-04 0
6: 2016-07-11 213257
7: 2016-07-18 513123
8: 2016-07-25 162171
9: 2016-08-01 162171
10: 2016-08-08 462171
11: 2016-08-15 0
12: 2016-08-22 0
13: 2016-08-29 0
14: 2016-09-05 0
15: 2016-09-12 0
16: 2016-09-19 0
17: 2016-09-26 0
As we can see the values are daily. I would like to convert these values to daily and at the date column to have the "daily date". So for instance, row 6 of dt, should be replaced by 7 rows, where in the date
column should be the dates
2016-07-11
until 2016-07-17
, and in the value column, for each of these rows, it should have 213257 divided by 7
Upvotes: 0
Views: 197
Reputation: 2070
This works as well:
dt[rep(1:nrow(dt),each=7)][,`:=`(date=date+seq_len(.N)-1,value=value/7),by=date]
Upvotes: 1
Reputation: 4482
dt <- dt[,.(date=ymd(seq.Date(from = as.Date(date),to = as.Date(date)+6,
by= "day" )), value=value/7),by="date"]
This also works, but i end up with 2 columns named date
, so i guess before doing this i should rename the date
column to date_temp
or something.
Upvotes: 0