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

时间:2018-09-26 20:25:29

标签: r time schedule

如果我有这样的数据:

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

我想将其转换为这种格式,如何在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

1 个答案:

答案 0 :(得分:0)

这是一种方法:

#  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))