簡體   English   中英

在R中創建一系列時間數據

[英]Create series of time data in R

如果我有這樣的數據:

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

這是一種方法:

#  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