rlubridatemutate

Add a new variable to dataframe after applying lubridate to datetime


I have a dataframe df1 with DateTime variable but the time interval is not exactly on the quarter hour which I need e.g.2024-07-17 13:00:00. I have used lubridate to round the DateTime to 15 minutes but I don't know how to add a new column to the dataframe with this rounded DateTime15. From searching stackoverflow I think mutate might be the answer (Add new variable created with mutate_ to data frame instead of creating a tibble) but I am new to R and am not sure how to construct it.

set the timezone

Sys.setenv(TZ='UTC')

Dataframe:

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

Solution

  • @Rui Barradas is totally right, but if you want to use mutate (not much value here but good to know for the future) you can do this. Part of your issue is likely because you're not naming the dataset anything, so it's not actually in your environment to access in future lines (hence the df <- I added to the beginning.

    df <- 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)
    library(tidyverse)
    df1 <-  mutate(df, RoundedTime = lubridate::round_date(DateTime, unit = '15 minutes'))