gpt4 book ai didi

r - 根据R中的条件计算日期之间的平均差

转载 作者:行者123 更新时间:2023-12-03 21:16:02 25 4
gpt4 key购买 nike

我有这个数据集:

Date New_Renew
2019-01-10 22:11:16 Renewing
2019-02-23 00:21:48 Renewing
2019-03-05 05:26:17 Renewing
2019-04-18 15:05:10 NEW
2019-04-18 15:07:52 NEW
2019-04-26 11:32:25 Renewing
2019-05-03 14:15:25 Renewing
2019-05-08 21:10:08 NEW
2019-05-16 13:35:57 Renewing
2019-05-24 13:18:23 Renewing
2019-06-01 12:42:21 Renewing
2019-06-17 18:08:09 Renewing
2019-06-26 13:40:29 Renewing
2019-12-13 17:57:43 Renewing
2020-01-03 11:49:14 Renewing
2020-01-11 11:46:51 Renewing
2020-01-14 21:08:08 NEW
2020-01-18 21:14:30 NEW
2020-01-21 16:08:37 NEW
2020-01-28 11:41:44 Renewing
2020-01-30 13:34:21 Renewing
2020-02-03 13:29:37 Renewing
2020-02-18 17:15:52 Renewing
2020-02-20 13:37:52 Renewing
2020-02-24 12:55:25 Renewing
2020-02-26 21:13:38 NEW
2020-03-04 13:23:41 Renewing
2020-03-09 16:48:36 Renewing

我想要的是,当 New_Renew 变量等于 NEW 时,计算与 NEW 相关联的日期之间差异的平均值。简而言之,用户进行新交易的频率。

最佳答案

使用 aggregatediff . 60*24将产生的秒数转换为天数。

aggregate(Date ~ New_Renew, dat, function(x) mean(diff(x))/(60*24))
# New_Renew Date
# 1 NEW 52.38292438
# 2 Renewing 0.01471444

或与 tapply .
with(dat, tapply(Date, New_Renew, function(x) mean(diff(x))/(60*24)))
# NEW Renewing
# 52.38292438 0.01471444

数据
dat <- structure(list(Date = structure(c(1547154676, 1550877708, 1551759977, 
1555592710, 1555592872, 1556271145, 1556885725, 1557342608, 1558006557,
1558696703, 1559385741, 1560787689, 1561549229, 1576256263, 1578048554,
1578739611, 1579032488, 1579378470, 1579619317, 1580208104, 1580387661,
1580732977, 1582042552, 1582202272, 1582545325, 1582748018, 1583324621,
1583768916), class = c("POSIXct", "POSIXt"), tzone = ""), New_Renew = c("Renewing",
"Renewing", "Renewing", "NEW", "NEW", "Renewing", "Renewing",
"NEW", "Renewing", "Renewing", "Renewing", "Renewing", "Renewing",
"Renewing", "Renewing", "Renewing", "NEW", "NEW", "NEW", "Renewing",
"Renewing", "Renewing", "Renewing", "Renewing", "Renewing", "NEW",
"Renewing", "Renewing")), row.names = c(NA, -28L), class = "data.frame")

关于r - 根据R中的条件计算日期之间的平均差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60619535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com