简体   繁体   English

在R中创建一系列时间数据

[英]Create series of time data in R

If I have data like this: 如果我有这样的数据:

x y Z Frequency(minutes) start_time End_time
1 2 1    7                00:06:00   06:21:00

and I want to convert it to this format, how to do it in R? 我想将其转换为这种格式,如何在R中执行呢?

x y Z Frequency(minutes) start_time End_time  New_time
1 2 1      7             06:00:00   06:21:00  06:07:00
1 2 1      7             06:00:00   06:21:00  06:14:00
1 2 1      7             06:00:00   06:21:00  06:21:00

Here's an approach: 这是一种方法:

#  First pull in data
df <- read.table(header = T, text = "
                 x y z frequency_min start_time end_time
1 2 1    7                06:00:00   06:21:00") %>%

  # Reformat timestamps
  mutate(start_time2 = lubridate::ymd_hms(paste(Sys.Date(), start_time)),
         end_time2 = lubridate::ymd_hms(paste(Sys.Date(), end_time))) %>%

  # Figure out how many rows needed of each
  mutate(copies = (end_time2 - start_time2) / 
           lubridate::dminutes(1) / frequency_min) %>%

  # Make this many copies
  uncount(copies, .id = "copy_id") %>%

  # Add requested 'new_time' column
  mutate(new_time = start_time2 + frequency_min * copy_id * dminutes(1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM