提问人:Mulisa Dida 提问时间:5/29/2023 最后编辑:Brian Tompsett - 汤莱恩Mulisa Dida 更新时间:5/30/2023 访问量:31
在 R 中使用开始和结束时间按时间间隔对数据进行排序
Sorting data by time interval using start and end times in R
问:
我有几个按日期和时间收集的数据。收集的数据是按时间间隔收集的。我正在通过传感器进行气体分析。因此,传感器以以下形式给了我数据
RFID `Farm Name` `Start Time` `End Time`
<dbl> <dbl> <chr> <chr>
1 9.82e14 9.82e14 1/09/2022 0:34:15 1/09/2022 0:43:19
2 9.82e14 9.82e14 1/09/2022 0:47:31 1/09/2022 0:53:12
3 9.82e14 9.82e14 1/09/2022 0:59:33 1/09/2022 1:03:01
4 9.82e14 9.82e14 1/09/2022 1:46:14 1/09/2022 1:49:27
5 9.82e14 9.82e14 1/09/2022 1:58:17 1/09/2022 2:08:25
6 9.82e14 9.82e14 1/09/2022 5:40:06 1/09/2022 5:48:05
7 9.82e14 9.82e14 1/09/2022 6:03:59 1/09/2022 6:12:57
8 9.82e14 9.82e14 1/09/2022 6:18:38 1/09/2022 6:21:06
9 9.82e14 9.82e14 1/09/2022 6:23:10 1/09/2022 6:25:15
10 9.82e14 9.82e14 1/09/2022 6:27:42 1/09/2022 6:33:09
我需要使用每行的开始时间和结束时间按顺序(以秒为单位)放置我的数据。那么我该怎么做呢?这意味着.1/09/2022 0:34:15, 1/09/2022 0:34:16, 1/09/2022 0:34:17......
此外,我还想使用开始和结束时间按顺序将此日期和时间按一列排列。喜欢这个:
2022-1-09 0:34:15, 2022-1-09 0:34:16, 2022-1-09 0:34:17, 2022-1-09 0:34:18,
..........
答:
0赞
Abdullah Faqih
5/30/2023
#1
您可以使用下面的代码来给出所需的结果,假设数据已经按开始和结束时间排序。
library(tidyverse)
df <- tibble(RFID = c(9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14),
`Farm Name` = c(9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14, 9.82e14),
`Start Time` = c("1/09/2022 0:34:15","1/09/2022 0:47:31","1/09/2022 0:59:33","1/09/2022 1:46:14","1/09/2022 1:58:17","1/09/2022 5:40:06","1/09/2022 6:03:59","1/09/2022 6:18:38","1/09/2022 6:23:10","1/09/2022 6:27:42"),
`End Time` = c("1/09/2022 0:43:19","1/09/2022 0:53:12","1/09/2022 1:03:01","1/09/2022 1:49:27","1/09/2022 2:08:25","1/09/2022 5:48:05","1/09/2022 6:12:57","1/09/2022 6:21:06","1/09/2022 6:25:15","1/09/2022 6:33:09"))
df <- df |>
# convert it into dttm
mutate(`Start Time` = dmy_hms(`Start Time`), `End Time` = dmy_hms(`End Time`)) |>
# create a sequence of time in 1 second increment based on the start and end time
mutate(time_sequence = map2(`Start Time`, `End Time`, ~seq(from = .x, to = .y, by = "1 sec"))) |>
# unnest it
unnest(time_sequence)
评论
0赞
Mulisa Dida
5/30/2023
谢谢你,阿卜杜拉。但是我需要在一列中有一个时间序列,从开始和结束时间开始,以一秒为增量。当我使用您的脚本时,我无法获取该列。那么,如何按顺序更改每行一列的开始和结束时间呢?
0赞
Abdullah Faqih
5/30/2023
您可以从最后一列中获取该列
评论