Extract a series of cases from a data frame for complete data sets

I have a data frame consisting of 5 variables (class in brackets)

1) DateTime (as.POSIXct), 2) ID (character), 3) Sensor 1 (numerical), 4) Sensor 2 (numerical), 5) Sensor 3 (numerical)

These data were collected from 5 tagged fish. Each fish has one label with three sensors on it, and each sensor has a unique identifier (thus, 5 bits of 4 identifiers / tags = 15 unique identifiers). Sensors record measurements related to each other, and thus record these measurements simultaneously. Measurement data is sent in the same order each time (ID = A, then B, then C). This data is sent to listening receivers, which can receive only one transmission at a time. To avoid the simultaneous addition of several tags and possibly never receiving data, each sensor is sent at an arbitrary interval (between 2-4 minutes) until a new set of measurements is collected and the cycle is restarted. But at random intervals, sometimes several tags try to send data at the same time,and therefore, these measurements will not be recorded. Sample data for one fish below:

> head(dat,15)

                   DateTime ID  Sensor1 Sensor2  Sensor3
    446 2015-05-15 19:05:41  B       NA    10.2       NA
    464 2015-05-15 19:14:20  B       NA    10.2       NA
    475 2015-05-15 19:17:32  C       NA      NA 10.58824
    486 2015-05-15 19:19:52  A 1.999499      NA       NA
    499 2015-05-15 19:22:31  B       NA    10.2       NA
    515 2015-05-15 19:28:10  A 1.999499      NA       NA
    523 2015-05-15 19:30:56  B       NA    10.1       NA
    542 2015-05-15 19:37:22  A 1.999499      NA       NA
    559 2015-05-15 19:41:09  B       NA    10.2       NA
    574 2015-05-15 19:44:47  C       NA      NA 10.50980
    613 2015-05-15 19:50:23  B       NA    10.3       NA
    633 2015-05-15 19:53:07  C       NA      NA 10.50980
    650 2015-05-15 19:56:32  A 1.999499      NA       NA
    684 2015-05-15 20:02:49  C       NA      NA 10.50980
    702 2015-05-15 20:05:51  A 1.999499      NA       NA

, , A, B C , . , . , (, 542, 559 574.)

, , , , 3 . ID A C, , , , , ( , ).

for dat, , . , R , , , . , , . ( , True False == TRUE, , ):

#make blank dataframe    
output <- data.frame (DateTime=rep(as.POSIXct(NA, tz="UTC"), length(tag123o$Transmitter)),
                          ID=rep(as.character(NA), length(tag123o$Transmitter)),
                          Sensor1=rep(as.numeric(NA), length(tag123o$Transmitter)),
                          Sensor2=rep(as.numeric(NA), length(tag123o$Transmitter)),
                          Sensor3=rep(as.numeric(NA), length(tag123o$Transmitter)))

    for (i in 1:length(dat$ID)) {
      if (((dat[i,names(dat)=="ID"] == "A69-1105-123") &
        (dat[i+1,names(dat)=="ID"] == "A69-1105-124") &
          (dat[i+2,names(dat)=="ID"] == "A69-1105-125"))==TRUE) {
            output[i,] <- cbind(dat[i,], data.frame(Cycle=i)) 
            output[i+1,] <- cbind(dat[i+1,], data.frame (Cycle=i))
            output[i+2,] <- cbind(dat[i+2,], data.frame(Cycle=i))
          }
    }
+4
1

"ABC" :

(matches <- gregexpr("ABC", paste(dat$ID, collapse=""))[[1]])
# [1] 8
# ...

, 8. , Sensor1 matches, Sensor2 matches+1, Sensor3 matches+2. , :

data.frame(DateTime1 = dat$DateTime[matches],
           DateTime2 = dat$DateTime[matches+1],
           DateTime3 = dat$DateTime[matches+2],
           Sensor1 = dat$Sensor1[matches],
           Sensor2 = dat$Sensor2[matches+1],
           Sensor3 = dat$Sensor3[matches+2])
#             DateTime1           DateTime2           DateTime3  Sensor1 Sensor2 Sensor3
# 1 2015-05-15 19:37:22 2015-05-15 19:41:09 2015-05-15 19:44:47 1.999499    10.2 10.5098

, (, , ).

+3

All Articles