Function/loop to create data frame for continuous time series spanning months/years

I'm looking at air quality data and trying to create a continuous time series from weekly 5 min data that I update every week. I have been creating weekly plots, but now my PI wants to see a continuous time series and I'm not sure how to stitch the data together.

Each dataset goes from Tuesday at 6 am to the following Tuesday at 6 am for one particular monitoring site (the eventual goal being to plot multiple sites at once to check for correlation, etc).

The code that I had attempted to use (taken from the Coursera course that I was taking while on a free trial) was this, and doesn't appear to work.

polluplot <- function(directory, pollutant, id = 1:6){
        files.list <- list.files(directory, full.names = TRUE)
        
        dat <- data.frame()
        
        for(i in id){
                dat <- rbind(dat, read.csv(files.list[i]))
        }
        
        pollplots <- plot(dat[, "CO"], na.rm = TRUE)
        
        return(pollplots)
        
        
}

Link to example files: Link

This made a single data frame of 6051 rows from your three files. I had to edit the file for Dec 13-20 because it has a row under the header listing the units and that causes all of the columns to be characters. That file is also missing the first column name, Date_Time, so I inserted that.

library(purrr)
FILES <- list.files("~/R/Play/Files/PositHelp",pattern = "csv$",full.names = TRUE)
AllDat <- map_dfr(FILES,read.csv)

I have not looked at your code yet but I think I have found a problem.

Your column names are inconsistent, giving you character values where I think you want numeric.

I read the files in as below

df13 <- read.csv("AMS-1 Dec 13 - 20 2022 5 min.csv")
df14 <- read.csv("AMS-1 Dec 6 - 13 2022 5 min.csv")
nf29 <- read.csv("AMS-1 Nov 29 - Dec 6 2022 5 min.csv")


> head(df13)
                          X   CO   SO2  TRS  H2S  THC  CH4 NMHC   NO  NO2  NOX    O3  NH3 PM2.5    RH    WS    WD    GR         LW  AT.2m   CO2 PC
1                            ppm   ppb  ppb  ppb  ppm  ppm  ppm  ppb  ppb  ppb   ppb  ppb ug/m3     %  km/h   deg  W/m2 % of range      C   ppm mm
2 December 13 2022  6:00:00 0.28 10.02  0.5 0.55 2.08 2.08    0  0.2 7.88 8.08 21.69    5  9.28 74.24 12.61 177.5 -0.32      -0.11  -13.1 433.5  0
3 December 13 2022  6:05:00 0.25  9.11 0.54 0.58 2.08 2.08    0 0.21 8.27 8.48 21.66 4.33  9.59  74.3 12.86 173.3 -0.28      -0.14  -13.1 434.1  0
4 December 13 2022  6:10:00 0.26  8.89 0.53 0.53 2.09 2.09    0 0.17 8.21 8.38 21.63 3.89  9.69 74.23  13.3 174.6 -0.22      -0.14 -13.04 433.9  0
5 December 13 2022  6:15:00 0.29  7.74 0.49 0.52 2.09 2.09    0 0.17 8.13  8.3 21.95 3.66  9.67 74.43    11 172.2 -0.17      -0.14    -13 433.1  0
6 December 13 2022  6:20:00 0.29  8.05 0.47 0.56 2.09 2.09    0 0.22 7.72 7.94 22.63 3.57  9.63 74.06 14.87 176.6 -0.21      -0.14    -13 432.6  0
> head(df14)
                 Date_Time   CO  SO2  TRS  H2S  THC  CH4 NMHC   NO   NO2   NOX    O3  NH3 PM2.5    RH   WS    WD    GR   LW  AT.2m   CO2 PC
1 December 6 2022  6:00:00 0.25 0.39 0.41 0.41 2.04 2.04    0 0.53 11.57 12.10 13.81 1.34 12.66 65.15 3.30 190.0 -0.11 0.41 -35.35 438.7  0
2 December 6 2022  6:05:00 0.23 0.50 0.39 0.45 2.04 2.04    0 0.30  8.89  9.19 16.13 1.22 11.26 64.89 3.31 186.2  0.01 0.41 -35.68 435.1  0
3 December 6 2022  6:10:00 0.22 0.15 0.37 0.44 2.04 2.04    0 0.34  9.10  9.45 17.22 1.21 12.98 65.04 3.99 192.8 -0.35 0.39 -35.45 432.5  0
4 December 6 2022  6:15:00 0.21 0.26 0.35 0.41 2.04 2.04    0 0.38 10.41 10.79 15.54 1.12 11.67 64.94 3.55 213.5 -0.44 0.36 -35.50 434.7  0
5 December 6 2022  6:20:00 0.27 0.40 0.39 0.46 2.04 2.04    0 0.27  8.34  8.61 16.53 0.97 14.12 64.46 3.60 215.5 -0.68 0.39 -35.95 433.6  0
6 December 6 2022  6:25:00 0.25 0.14 0.37 0.44 2.04 2.04    0 0.23  6.95  7.19 18.32 0.95 14.79 64.70 3.68 210.3 -0.09 0.39 -35.65 430.9  0
> head(nf29)
        Date_Time   CO  SO2  TRS  H2S  THC  CH4 NMHC NO  NO2  NOX    O3  NH3 PM2.5    RH    WS    WD    GR   LW  AT.2m   CO2   PC
1 2022-11-29 6:00 0.14 0.18 0.57 1.08 2.17 2.17    0 NA 8.57 8.32 22.29 0.56  3.76 73.38 15.71 18.74 -0.31 -0.2 -18.55 419.1 0.00
2 2022-11-29 6:05 0.14 0.16 0.51 0.82 2.16 2.16    0 NA 8.37 8.18 22.57 0.63  3.69 73.05 16.59 13.34 -0.24 -0.2 -18.60 419.5 0.00
3 2022-11-29 6:10 0.14 0.27 0.44 0.69 2.17 2.17    0 NA 8.63 8.38 22.38 0.58  3.52 73.10 15.52 18.37 -0.25 -0.2 -18.63 420.0 0.00
4 2022-11-29 6:15 0.14 0.17 0.52 0.69 2.20 2.20    0 NA 9.19 8.96 21.89 0.48  3.52 73.30 15.65 11.11 -0.22 -0.2 -18.70 420.1 0.00
5 2022-11-29 6:20 0.14 0.15 0.46 0.61 2.21 2.21    0 NA 8.82 8.54 22.51 0.40  3.34 73.53 15.02  8.60 -0.25 -0.2 -18.75 419.5 0.05
6 2022-11-29 6:25 0.14 0.17 0.48 0.56 2.17 2.17    0 NA 8.65 8.39 22.23 0.41  3.29 73.59 14.60 11.30 -0.27 -0.2 -18.80 419.2 0.00

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.