R 根据一个月内的天数增加小时数
我想根据天数增加小时数,并将这些小时数放在数据框中 例如,一开始我就有R 根据一个月内的天数增加小时数,r,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/
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
2017年9月的相同迭代
这是我正在寻找的输出
2016-08-19 23小时。。。2016-08-20 47小时……2016-08-20 65小时
以下是我的工作时间格式:
> 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")
>
首先,我们计算开始日期,然后得到每个日期的小时差:
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
start\u datedate\u entry6h+24h=30h
您想要什么输出?你真的需要字符串:11/09/2017 8h00+24h=32h
?那么…你基本上想在每小时中增加24*(自分钟(日期)起的天数)?@Hack-R我已经把我的data@PoGibas我想要的产量是每小时增加一个小时,比如2016-08-19 23小时。。。2016-08-20 47小时……2016-08-20 65小时
date_entry <- strptime(c("2017-09-10 03:45:19",
"2017-09-10 05:45:19",
"2017-09-11 06:45:19",
"2017-09-11 08:45:19",
"2017-09-12 09:45:19",
"2017-09-13 15:45:19"), "%Y-%m-%d %H:%M:%S")
df <- data.frame(date_entry)
df$start <- format(min(df$date_entry), "%Y-%m-%d")
df$hour_entry <- hour(date_entry)
# df$date <- format(df$date_entry, "%Y-%m-%d")
df$date_diff <- as.Date(as.character(df$date_entry), format="%Y-%m-%d")-
as.Date(as.character(df$start), format="%Y-%m-%d")
df$hour_diff <- paste0(as.character(df$hour_entry + 24*df$date_diff), "h")