gpt4 book ai didi

r - 拼接时间间隔 posixct

转载 作者:行者123 更新时间:2023-12-02 04:13:28 26 4
gpt4 key购买 nike

我有以下时间间隔,我想将其分成 10 个等间隔的实例。

head(data)             stoptime           starttime1 2014-08-19 14:52:04 2014-08-19 15:22:042 2014-08-19 16:27:14 2014-08-19 17:17:333 2014-08-19 18:05:59 2014-08-19 18:09:124 2014-08-19 17:25:35 2014-08-19 17:29:065 2014-08-19 18:23:29 2014-08-19 18:57:346 2014-08-19 07:39:15 2014-08-19 07:48:49

我可以使用此代码获取中点

one_day$midtime = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) /2 , origin = '1970-01-01')

但是,当我尝试将此代码扩展到十个等距实例时,它完全错误。为什么会发生这种情况以及如何修复此代码?

one_day$first = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .1 , origin = '1970-01-01')one_day$second = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .2, origin = '1970-01-01')one_day$thrid = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .3, origin = '1970-01-01')one_day$fourth = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .4, origin = '1970-01-01')one_day$fifth = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .5, origin = '1970-01-01')one_day$sixth = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .6, origin = '1970-01-01')one_day$seventh = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .7, origin = '1970-01-01')one_day$eighth = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .8, origin = '1970-01-01')one_day$ninth = as.POSIXct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .9, origin = '1970-01-01')
head(one_day)  diff.time            stoptime           starttime             midtime               first1      1800 2014-08-19 14:52:04 2014-08-19 15:22:04 2014-08-19 15:07:04 1978-12-05 03:49:242      3019 2014-08-19 16:27:14 2014-08-19 17:17:33 2014-08-19 16:52:23 1978-12-05 04:10:283       193 2014-08-19 18:05:59 2014-08-19 18:09:12 2014-08-19 18:07:35 1978-12-05 04:25:314       211 2014-08-19 17:25:35 2014-08-19 17:29:06 2014-08-19 17:27:20 1978-12-05 04:17:285      2045 2014-08-19 18:23:29 2014-08-19 18:57:34 2014-08-19 18:40:31 1978-12-05 04:32:066       574 2014-08-19 07:39:15 2014-08-19 07:48:49 2014-08-19 07:44:02 1978-12-05 02:20:48               second               thrid              fourth               fifth               sixth1 1987-11-08 12:38:49 1996-10-11 21:28:14 2005-09-15 06:17:39 2014-08-19 15:07:04 2023-07-23 23:56:282 1987-11-08 13:20:57 1996-10-11 22:31:26 2005-09-15 07:41:54 2014-08-19 16:52:23 2023-07-24 02:02:523 1987-11-08 13:51:02 1996-10-11 23:16:33 2005-09-15 08:42:04 2014-08-19 18:07:35 2023-07-24 03:33:064 1987-11-08 13:34:56 1996-10-11 22:52:24 2005-09-15 08:09:52 2014-08-19 17:27:20 2023-07-24 02:44:485 1987-11-08 14:04:12 1996-10-11 23:36:18 2005-09-15 09:08:25 2014-08-19 18:40:31 2023-07-24 04:12:376 1987-11-08 09:41:36 1996-10-11 17:02:25 2005-09-15 00:23:13 2014-08-19 07:44:02 2023-07-23 15:04:50              seventh              eighth               ninth1 2032-06-26 08:45:53 2041-05-30 17:35:18 2050-05-04 02:24:432 2032-06-26 11:13:20 2041-05-30 20:23:49 2050-05-04 05:34:183 2032-06-26 12:58:37 2041-05-30 22:24:08 2050-05-04 07:49:394 2032-06-26 12:02:16 2041-05-30 21:19:44 2050-05-04 06:37:125 2032-06-26 13:44:44 2041-05-30 23:16:50 2050-05-04 08:48:566 2032-06-25 22:25:38 2041-05-30 05:46:27 2050-05-03 13:07:15
dput(data1)structure(list(stoptime = structure(c(1408477924, 1408483634, 1408489559, 1408487135, 1408490609, 1408451955, 1408452727, 1408498708, 1408486644, 1408454996), class = c("POSIXct", "POSIXt"), tzone = "EST"),     starttime = structure(c(1408479724, 1408486653, 1408489752,     1408487346, 1408492654, 1408452529, 1408455826, 1408501153,     1408488389, 1408458514), class = c("POSIXct", "POSIXt"), tzone = "EST")), .Names = c("stoptime", "starttime"), row.names = c(NA, 10L), class = "data.frame")

最佳答案

1:序列

首先,您必须将数据帧的列转换为 POSIXct 或 POSIXlt 类,因为 r 基函数 seq 有一个用于该类对象的方法。

看看这个简化的代码:

library(lubridate)
a <- "2014-08-19 14:52:04"
b <- "2014-08-19 15:22:04"

a <- ymd_hms(a)
b <- ymd_hms(b)

a
[1] "2014-08-19 14:52:04 UTC"
b
[1] "2014-08-19 15:22:04 UTC"

然后您只需使用 seq 函数并将参数 length.out 设置为您要查找的序列的值。代码将自动创建一个从开始到结束均分的值序列。

seq(a, b, length.out = 10)
[1] "2014-08-19 14:52:04 UTC" "2014-08-19 14:55:24 UTC"
[3] "2014-08-19 14:58:44 UTC" "2014-08-19 15:02:04 UTC"
[5] "2014-08-19 15:05:24 UTC" "2014-08-19 15:08:44 UTC"
[7] "2014-08-19 15:12:04 UTC" "2014-08-19 15:15:24 UTC"
[9] "2014-08-19 15:18:44 UTC" "2014-08-19 15:22:04 UTC"

2:矢量化步骤 1

既然您知道如何实现目标,那么只需尝试如何根据值对其进行矢量化即可。

我敢打赌有几种方法,这里是一种。使用 maply 函数,您可以循环遍历元素并将(第一个对象的)第一个元素与(第二个对象的)第一个元素匹配,依此类推。请记住,您必须使用 MoreArg 参数指定哪些参数是固定的。

这是代码:

mapply(seq,
to = data1$starttime,
from = data1$stoptime,
MoreArgs = list(length.out = 10),
SIMPLIFY = F)

这会生成您想要的数据列表,但遗憾的是不是所需的格式:

[[1]]
[1] "2014-08-19 14:52:04 UTC" "2014-08-19 14:55:24 UTC"
[3] "2014-08-19 14:58:44 UTC" "2014-08-19 15:02:04 UTC"
[5] "2014-08-19 15:05:24 UTC" "2014-08-19 15:08:44 UTC"
[7] "2014-08-19 15:12:04 UTC" "2014-08-19 15:15:24 UTC"
[9] "2014-08-19 15:18:44 UTC" "2014-08-19 15:22:04 UTC"

[[2]]
[1] "2014-08-19 16:27:14 UTC" "2014-08-19 16:32:49 UTC"
[3] "2014-08-19 16:38:24 UTC" "2014-08-19 16:44:00 UTC"
[5] "2014-08-19 16:49:35 UTC" "2014-08-19 16:55:11 UTC"
[7] "2014-08-19 17:00:46 UTC" "2014-08-19 17:06:22 UTC"
[9] "2014-08-19 17:11:57 UTC" "2014-08-19 17:17:33 UTC"

[[3]]
[1] "2014-08-19 18:05:59 UTC" "2014-08-19 18:06:20 UTC"
[3] "2014-08-19 18:06:41 UTC" "2014-08-19 18:07:03 UTC"
[5] "2014-08-19 18:07:24 UTC" "2014-08-19 18:07:46 UTC"
[7] "2014-08-19 18:08:07 UTC" "2014-08-19 18:08:29 UTC"
[9] "2014-08-19 18:08:50 UTC" "2014-08-19 18:09:12 UTC"

[[4]]
[1] "2014-08-19 17:25:35 UTC" "2014-08-19 17:25:58 UTC"
[3] "2014-08-19 17:26:21 UTC" "2014-08-19 17:26:45 UTC"
[5] "2014-08-19 17:27:08 UTC" "2014-08-19 17:27:32 UTC"
[7] "2014-08-19 17:27:55 UTC" "2014-08-19 17:28:19 UTC"
[9] "2014-08-19 17:28:42 UTC" "2014-08-19 17:29:06 UTC"

[[5]]
[1] "2014-08-19 18:23:29 UTC" "2014-08-19 18:27:16 UTC"
[3] "2014-08-19 18:31:03 UTC" "2014-08-19 18:34:50 UTC"
[5] "2014-08-19 18:38:37 UTC" "2014-08-19 18:42:25 UTC"
[7] "2014-08-19 18:46:12 UTC" "2014-08-19 18:49:59 UTC"
[9] "2014-08-19 18:53:46 UTC" "2014-08-19 18:57:34 UTC"

[[6]]
[1] "2014-08-19 07:39:15 UTC" "2014-08-19 07:40:18 UTC"
[3] "2014-08-19 07:41:22 UTC" "2014-08-19 07:42:26 UTC"
[5] "2014-08-19 07:43:30 UTC" "2014-08-19 07:44:33 UTC"
[7] "2014-08-19 07:45:37 UTC" "2014-08-19 07:46:41 UTC"
[9] "2014-08-19 07:47:45 UTC" "2014-08-19 07:48:49 UTC"

[[7]]
[1] "2014-08-19 07:52:07 UTC" "2014-08-19 07:57:51 UTC"
[3] "2014-08-19 08:03:35 UTC" "2014-08-19 08:09:20 UTC"
[5] "2014-08-19 08:15:04 UTC" "2014-08-19 08:20:48 UTC"
[7] "2014-08-19 08:26:33 UTC" "2014-08-19 08:32:17 UTC"
[9] "2014-08-19 08:38:01 UTC" "2014-08-19 08:43:46 UTC"

[[8]]
[1] "2014-08-19 20:38:28 UTC" "2014-08-19 20:42:59 UTC"
[3] "2014-08-19 20:47:31 UTC" "2014-08-19 20:52:03 UTC"
[5] "2014-08-19 20:56:34 UTC" "2014-08-19 21:01:06 UTC"
[7] "2014-08-19 21:05:38 UTC" "2014-08-19 21:10:09 UTC"
[9] "2014-08-19 21:14:41 UTC" "2014-08-19 21:19:13 UTC"

[[9]]
[1] "2014-08-19 17:17:24 UTC" "2014-08-19 17:20:37 UTC"
[3] "2014-08-19 17:23:51 UTC" "2014-08-19 17:27:05 UTC"
[5] "2014-08-19 17:30:19 UTC" "2014-08-19 17:33:33 UTC"
[7] "2014-08-19 17:36:47 UTC" "2014-08-19 17:40:01 UTC"
[9] "2014-08-19 17:43:15 UTC" "2014-08-19 17:46:29 UTC"

[[10]]
[1] "2014-08-19 08:29:56 UTC" "2014-08-19 08:36:26 UTC"
[3] "2014-08-19 08:42:57 UTC" "2014-08-19 08:49:28 UTC"
[5] "2014-08-19 08:55:59 UTC" "2014-08-19 09:02:30 UTC"
[7] "2014-08-19 09:09:01 UTC" "2014-08-19 09:15:32 UTC"
[9] "2014-08-19 09:22:03 UTC" "2014-08-19 09:28:34 UTC"

在这一点上,我想这只是相同数据操作的问题,但我(现在)找不到办法。

关于r - 拼接时间间隔 posixct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35233543/

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