0

让我的时间序列数据成为

df <- data.frame(timestamp = c('2021-08-11 12:00:00', '2021-08-11 01:00:00', '2021-08-11 02:00:00', '2021-08-11 03:00:00', '2021-08-11 04:00:00', '2021-08-11 05:00:00', '2021-08-11 06:00:00', '2021-08-11 07:00:00', '2021-08-11 08:00:00', '2021-08-11 09:00:00', '2021-08-11 10:00:00', '2021-08-11 11:00:00', '2021-08-11 12:00:00', '2021-08-11 01:00:00', '2021-08-11 02:00:00', '2021-08-11 03:00:00', '2021-08-11 04:00:00', '2021-08-11 05:00:00', '2021-08-11 06:00:00', '2021-08-11 07:00:00', '2021-08-11 08:00:00', '2021-08-11 09:00:00', '2021-08-11 10:00:00', '2021-08-11 11:00:00' ),
value = c(77,190,17,-4,163,158,25,-2,132,119,-35,165,99,-21,199,62,-1,30,123,42,56,-36,123,24))

我的需求数据框

df <- data.frame(timestamp = c('2021-08-11 00:00:00', '2021-08-11 01:00:00', '2021-08-11 02:00:00', '2021-08-11 03:00:00', '2021-08-11 04:00:00', '2021-08-11 05:00:00', '2021-08-11 06:00:00', '2021-08-11 07:00:00', '2021-08-11 08:00:00', '2021-08-11 09:00:00', '2021-08-11 10:00:00', '2021-08-11 11:00:00', '2021-08-11 12:00:00', '2021-08-11 13:00:00', '2021-08-11 14:00:00', '2021-08-11 15:00:00', '2021-08-11 16:00:00', '2021-08-11 17:00:00', '2021-08-11 18:00:00', '2021-08-11 19:00:00', '2021-08-11 20:00:00', '2021-08-11 21:00:00', '2021-08-11 22:00:00', '2021-08-11 23:00:00' ),
                 value = c(77,190,17,-4,163,158,25,-2,132,119,-35,165,99,-21,199,62,-1,30,123,42,56,-36,123,24))

原始数据是每分钟或每秒的间隔。

这里是第一个

df$timestamp[1] = [1] "2021-08-11 12:00:00" 

是上午,即夜后 00 小时。

我想将时间戳列转换为 24 小时格式。

我想要这样的时间戳

4

1 回答 1

1

一个整洁的选择。

library(tidyverse)

df %>%
  mutate(id = str_sub(timestamp, 1, 10),
         timestamp = ymd_hms(timestamp)) %>%
  group_by(id) %>%
  mutate(timestamp = date(timestamp)+hours(row_number()-1)) %>%
  ungroup() %>%
  select(-id)
  
# # A tibble: 24 x 2
#     timestamp           value
#     <dttm>              <dbl>
#  1  2021-08-11 00:00:00    77
#  2  2021-08-11 01:00:00   190
#  3  2021-08-11 02:00:00    17
#  4  2021-08-11 03:00:00    -4
#  5  2021-08-11 04:00:00   163
#  6  2021-08-11 05:00:00   158
#  7  2021-08-11 06:00:00    25
#  8  2021-08-11 07:00:00    -2
#  9  2021-08-11 08:00:00   132
# 10  2021-08-11 09:00:00   119
# # … with 14 more rows
于 2021-11-16T18:42:14.990 回答