增加小时数取决于一个月内的天数

时间:2017-10-02 16:46:02

标签: r

我希望根据天数增加小时数,并将这些小时数放在数据框中。

比如说,我有

10/09/2017  3h00
10/09/2017  5h00
11/09/2017  6h00
11/09/2017  8h00
12/09/2017  9h00

然后我想拥有:

10/09/2017  3h00
10/09/2017  5h00
11/09/2017  6h00 + 24h =28h
11/09/2017  8h00 + 24h =32h
12/09/2017  9h00 + 48h =57h
13/09/2017  15h00 + 72h= 87h 

和本月/ 09/2017的相同迭代

这是我正在寻找的输出

2016-08-19 23h ... 2016-08-20 47h .... 2016-08-20 65h 这是我的工时格式:

 > dput(daily_data2)
structure(list(visitorID = c("16081918503913361", "16081918503913361", 
"16081918503913361", "16081918503913361", "16081920380127901", 
"16081920380127901", "16081920380127901", "16081920380127901", 
"16081920380127901", "16081920380127901", "16081920380127901", 
"16081920380127901", "16081921092401601", "16081921092401601", 
"16081921092401601", "16081921092401601", "16081921092401601", 
"16081921092401601", "16041014505621221", "16041014505621221", 
"16041014505621221", "16041014505621221", "16081523021881101", 
"16081523021881101", "16081523021881101", "16081523021881101", 
"16082009423468441", "16082009423468441", "16082009423468441", 
"16082009423468441"), variationID = c(190949L, 190949L, 190949L, 
190949L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 0L, 0L, 0L, 0L), categoryID = c(279L, 280L, 281L, 282L, 
279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 279L, 
280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 
279L, 280L, 281L, 282L), actionID = c(148156L, 148157L, 152184L, 
221911L, 838346L, 838347L, 116586L, 121437L, 214544L, 214545L, 
115960L, 123037L, 149591L, 149592L, 337193L, 337194L, 115960L, 
116590L, 125550L, 125551L, 115960L, 121891L, 185973L, 0L, 115960L, 
136482L, 875336L, 875337L, 115960L, 125404L), time = c("2016-08-19 17:45:19", 
"2016-08-19 17:45:19", "2016-08-19 17:45:19", "2016-08-19 17:45:19", 
"2016-08-19 18:59:19", "2016-08-19 18:59:19", "2016-08-19 18:59:19", 
"2016-08-19 18:59:19", "2016-08-19 18:59:19", "2016-08-19 18:59:19", 
"2016-08-19 18:59:19", "2016-08-19 18:59:19", "2016-08-19 19:27:07", 
"2016-08-19 19:27:07", "2016-08-19 19:27:07", "2016-08-19 19:27:07", 
"2016-08-19 19:27:07", "2016-08-19 19:27:07", "2016-08-19 23:33:46", 
"2016-08-19 23:33:46", "2016-08-19 23:33:46", "2016-08-19 23:33:46", 
"2016-08-20 07:13:32", "2016-08-20 07:13:32", "2016-08-20 07:13:32", 
"2016-08-20 07:13:32", "2016-08-20 08:52:12", "2016-08-20 08:52:12", 
"2016-08-20 08:52:12", "2016-08-20 08:52:12")), .Names = c("visitorID", 
"variationID", "categoryID", "actionID", "time"), row.names = c(NA, 
30L), class = "data.frame")
> 

2 个答案:

答案 0 :(得分:0)

首先我们计算start_date,然后得到每个日期的小时差异。:

start_date <- min(as.POSIXct(strptime(as.character(df$time),"%Y-%m-%d"))) 
df$hour <- paste0(round(difftime(df$time, start_date, units="hours"), 0),"h")
df


           visitorID variationID categoryID actionID                time hour
1  16081918503913361      190949        279   148156 2016-08-19 17:45:19  18h
2  16081918503913361      190949        280   148157 2016-08-19 17:45:19  18h
3  16081918503913361      190949        281   152184 2016-08-19 17:45:19  18h
4  16081918503913361      190949        282   221911 2016-08-19 17:45:19  18h
5  16081920380127901           0        279   838346 2016-08-19 18:59:19  19h
6  16081920380127901           0        280   838347 2016-08-19 18:59:19  19h
7  16081920380127901           0        281   116586 2016-08-19 18:59:19  19h
8  16081920380127901           0        282   121437 2016-08-19 18:59:19  19h
9  16081920380127901           0        279   214544 2016-08-19 18:59:19  19h
10 16081920380127901           0        280   214545 2016-08-19 18:59:19  19h
11 16081920380127901           0        281   115960 2016-08-19 18:59:19  19h
12 16081920380127901           0        282   123037 2016-08-19 18:59:19  19h
13 16081921092401601           0        279   149591 2016-08-19 19:27:07  19h
14 16081921092401601           0        280   149592 2016-08-19 19:27:07  19h
15 16081921092401601           0        279   337193 2016-08-19 19:27:07  19h
16 16081921092401601           0        280   337194 2016-08-19 19:27:07  19h
17 16081921092401601           0        281   115960 2016-08-19 19:27:07  19h
18 16081921092401601           0        282   116590 2016-08-19 19:27:07  19h
19 16041014505621221      190949        279   125550 2016-08-19 23:33:46  24h
20 16041014505621221      190949        280   125551 2016-08-19 23:33:46  24h
21 16041014505621221      190949        281   115960 2016-08-19 23:33:46  24h
22 16041014505621221      190949        282   121891 2016-08-19 23:33:46  24h
23 16081523021881101      190949        279   185973 2016-08-20 07:13:32  31h
24 16081523021881101      190949        280        0 2016-08-20 07:13:32  31h
25 16081523021881101      190949        281   115960 2016-08-20 07:13:32  31h
26 16081523021881101      190949        282   136482 2016-08-20 07:13:32  31h
27 16082009423468441           0        279   875336 2016-08-20 08:52:12  33h
28 16082009423468441           0        280   875337 2016-08-20 08:52:12  33h
29 16082009423468441           0        281   115960 2016-08-20 08:52:12  33h
30 16082009423468441           0        282   125404 2016-08-20 08:52:12  33h

答案 1 :(得分:0)

modifyCodeDiv