如何使用lubridate处理DateTime后为R数据框添加新列
问题描述
我有一个名为df1的数据框,其中的DateTime变量时间间隔并非所需的整点15分时刻(例如2024-07-17 13:00:00)。我已使用lubridate包将DateTime四舍五入到15分钟,但不知道如何将该四舍五入后的时间作为新列DateTime15添加到数据框中。通过搜索得知mutate函数可能解决问题,但作为R新手,不清楚具体写法。
设置时区
Sys.setenv(TZ='UTC')
数据框结构
structure(list(Tag = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), Date = structure(c(1721174400, 1721174400, 1721174400, 1721174400, 1721174400, 1721174400, 1721174400, 1721174400, 1721174400, 1721174400), tzone = "UTC", class = c("POSIXct", "POSIXt")), Time = c("13:00:20", "13:16:04", "13:31:05", "13:46:06", "14:01:08", "14:16:08", "14:31:09", "14:46:10", "15:01:11", "15:16:12" ), `Temp (C)` = c(17.9, 17.9, 17.9, 17.8, 17.8, 17.8, 17.8, 17.7, 17.7, 17.7), `Baro (mb)` = c(1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016), pH = c(8.45, 8.42, 8.4, 8.38, 8.38, 8.37, 8.37, 8.36, 8.36, 8.37), pHmV = c(-68.3, -67.1, -66.2, -65.2, -65, -64.8, -64.7, -64.4, -64.3, -64.5), `ORP (REDOX)` = c(225.8, 212.5, 221.1, 229.1, 234, 237.5, 239.6, 240.6, 242.3, 242.6), `DO (%Air Sat)` = c(107.4, 107.4, 106.6, 106.1, 106.3, 106.4, 106.5, 106.4, 106.7, 106.6), `DO (mg/L)` = c(10.16, 10.16, 10.08, 10.06, 10.08, 10.09, 10.09, 10.11, 10.14, 10.13), `EC (uS/cm @25C)` = c(563, 561, 562, 561, 563, 560, 564, 564, 564, 565), `RES (Ohms.cm)` = c(2053, 2061, 2057, 2066, 2057, 2070, 2053, 2057, 2057, 2053), `TDS (mg/L)` = c(365, 364, 365, 364, 365, 364, 366, 366, 366, 367), `SAL (ppt)` = c(0.24, 0.24, 0.24, 0.24, 0.24, 0.23, 0.24, 0.24, 0.24, 0.24), `SSG (st)` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `NH3 (mg/L)` = c(0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01), `CDOM (ug/L)` = c(8.9, 8.9, 9.1, 9, 7.9, 8.7, 9.2, 8.7, 8.6, 8.9), `Ammonium (mg/L)` = c(0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.12), DateTime = structure(c(1721217620, 1721218564, 1721219465, 1721220366, 1721221268, 1721222168, 1721223069, 1721223970, 1721224871, 1721225772), tzone = "", class = c("POSIXct", "POSIXt"))), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
现有代码
library(lubridate) dfX<-lubridate::round_date(df1, unit = '15 minutes')
解决方案
要给数据框添加新的四舍五入时间列,你需要用到dplyr包的mutate()函数(它是tidyverse生态的一部分,专门用于修改或添加数据框列)。具体步骤如下:
- 加载所需的包:
lubridate用于时间处理,dplyr用于数据框操作
library(lubridate) library(dplyr)
- 使用
mutate()添加新列DateTime15,对原列DateTime执行四舍五入操作:
df1 <- df1 %>% mutate(DateTime15 = round_date(DateTime, unit = "15 minutes"))
%>%是管道符,作用是把左边的df1传递给右边的mutate()函数处理round_date(DateTime, unit = "15 minutes")会把DateTime列的每个时间点四舍五入到最近的15分钟整点
- 验证结果:查看处理后的数据框前几行,确认
DateTime15列是否符合预期
head(df1 %>% select(DateTime, DateTime15))
输出示例:
DateTime DateTime15 1 2024-07-17 13:00:20 2024-07-17 13:00:00 2 2024-07-17 13:16:04 2024-07-17 13:15:00 3 2024-07-17 13:31:05 2024-07-17 13:30:00 4 2024-07-17 13:46:06 2024-07-17 13:45:00 5 2024-07-17 14:01:08 2024-07-17 14:00:00 6 2024-07-17 14:16:08 2024-07-17 14:15:00
注意事项
- 原数据的
DateTime列时区为空,建议统一设置为UTC(和你之前的时区设置保持一致),避免时区混乱:
df1$DateTime <- force_tz(df1$DateTime, tzone = "UTC")
- 如果不想覆盖原数据框,可以把结果赋值给新变量,比如
df_with_new_col <- df1 %>% mutate(...)
内容的提问来源于stack exchange,提问作者LZ24AP
相关产品推荐
相关产品推荐

