Search code examples
rtime-series

Expand a time series from every 15 minutes to 10 seconds in R


I have the following data set, abbreviated for simplicity:
SM = soil moisture, T = Temperature

DateTime              SMCount      T1        T2      T3  
2023-04-24 11:45:00   3960        12.875   15.625   17.5  
2023-04-24 12:00:00   3951        13.00    15.375   17.125         
2023-04-24 12:15:00   3955        13.25    15.6875  17.5  
2023-04-24 12:30:00   3961        13.1875  14.75    15.875 

etc.

I would like to create the following:

DateTime              SMCount      T1        T2      T3  
2023-04-24 11:45:00   3960        12.875   15.625   17.5  
2023-04-24 11:45:10   3960        12.875   15.625   17.5  
2023-04-24 11:45:20   3960        12.875   15.625   17.5  
2023-04-24 11:45:30   3960        12.875   15.625   17.5 

... and so on

2023-04-24 12:00:00   3951        13.00    15.375   17.125   
2023-04-24 12:00:10   3951        13.00    15.375   17.125   
2023-04-24 12:00:20   3951        13.00    15.375   17.125   
2023-04-24 12:00:30   3951        13.00    15.375   17.125   

... and so on

2023-04-24 12:15:00   3955        13.25    15.6875  17.5   
2023-04-24 12:15:10   3955        13.25    15.6875  17.5   
2023-04-24 12:15:20   3955        13.25    15.6875  17.5   
2023-04-24 12:15:30   3955        13.25    15.6875  17.5  

... and so on

2023-04-24 12:30:00   3961        13.1875  14.75    15.875   
2023-04-24 12:30:10   3961        13.1875  14.75    15.875   
2023-04-24 12:30:20   3961        13.1875  14.75    15.875   
2023-04-24 12:30:30   3961        13.1875  14.75    15.875   

... and so on

I searched for the problem and came across two possible solutions I was trying to adapt:

Solution 1:

library(tidyr)
df <- complete(soilmoisture$DateTime = seq(from = min(soilmoisture$DateTime), to = max(soilmoisture$DateTime), by = 1)) %>%
  fill(SMCount)

Solution 2:

library(tidyverse)
data <- mutate(soilmoisture$DateTime = lubridate::ymd_hms(soilmoisture$DateTime))%>%
  complete(soilmoisture$DateTime = seq.POSIXt(min(soilmoisture$Datetime), max(soilmoisture$DateTime), by="sec"))%>%
  fill(SMCount)

I'm getting the following error messages, respectively:

Error: unexpected '=' in "df %>% complete(soilmoisture$DateTime="  
and  
Error: unexpected '=' in "data <- mutate(soilmoisture$DateTime ="

Any idea how to fix it? And how do I adapt my code that so that SMCount, T1, T2 and T3 are all filled in at the same time?

Many thanks


Solution

  • I think you can try this, please let me know if this is what you were expecting

    data <- structure(list(DateTime = structure(c(1682336700, 1682337600, 
                                                 1682338500, 1682339400), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                          SMCount = c(3960L, 3951L, 3955L, 3961L), T1 = c(12.875, 13, 
                                                                          13.25, 13.1875), T2 = c(15.625, 15.375, 15.6875, 14.75), 
                          T3 = c(17.5, 17.125, 17.5, 15.875)), row.names = c(NA, -4L
                          ), class = "data.frame")
    

    Code

      dfs <- data %>% 
      complete(DateTime = seq(from = min(DateTime), to = max(DateTime), by = 1)) %>%
      fill(SMCount,T1, T2, T3,.direction='down')