samedi 23 février 2019

How to conditionally replace a discrete number of rows based on the value of a prior row

I have a large timeseries dataset that I'm trying to QC.

We're measuring CO2 in a marine setting and every now and then the instrument switches valves to rinse. The valve position is recorded in the data file. However everytime the valve switches from 6211 to 6218, I want to replace the following 90 CO2 measurements with "NA" because these data are bad. Each row represents 1 second of data. Here's what I've been messing around with:

# A tibble: 31,399 x 2
   `df$CO2_ppm` `df$ValvePos`
                <dbl>               <dbl>
 1               360.                6218
 2               355.                6218
 3               376.                6218
 4               439.                6218
 5               448.                6218
 6               444.                6218
 7               440.                6218
 8               436.                6218
 9               432.                6218
10               430.                6218

for (i in 1:length(df$ValvePos)) {
    if(rdf$ValvePos[[i]] == 6211 && df$ValvePos[[i+1]] == 6218) {
       df$CO2_uatm[[i:i+89]] <- NA
    }
}

My error message says

Error in $<-.data.frame(*tmp*, "pCO2_uatm", value = c(NA, NA, NA, : replacement has 3597 rows, data has 31399

I'm a little frustrated that I'm having difficulties with this so any help would be tremendous.

Aucun commentaire:

Enregistrer un commentaire