Search code examples
rif-statementreplacetime-series

Replacing sequence of row values in R


I am trying to replace a sequence of rows based on the previous and next status in the rows. Let's say I have three factors namely 'Control', 'Fan' and 'Compressor' in a time-series data. If the factor 'Fan' appears between 'Control' and 'Compressor', then I wanted to change the factor 'Fan' to 'Compressor'.

For example, In the below table, I wanted to replace 'Fan' with 'Control'. Note: In the original, column 'Status' contained 'character'. I tried converting the characters to factors, thinking it might help solve the problem. So does not matter if the desired output is a character or factor.

Status
Control
Control
Fan
Fan
Fan
Compressor
Compressor

Desired output:

Status
Control
Control
Compressor
Compressor
Compressor
Compressor
Compressor

Thanks.

I tried the following,

for(i in 2:nrow(df))
  df$Status[i]<- ifelse(df$Status[i]=='Fan' && df$Status[i+1]=='Compressor',
                                  "Compressor",df$Status_change1[i])

But this only replaces the value in one row.

The output I got with the above code:

Status
Control
Control
Fan
Fan
Compressor
Compressor
Compressor

I am struck in finding the next time/episode when the status ' Compressor' occurs.


Solution

  • Just for fun, I think this works as well just looping compressor locations, finding elements between fans and replacing. Great answer above.

    library(dplyr)
    Test <- data.frame(Status = c("Control", "Control", "Fan", "Fan", "Compressor", "Fan", "Fan",
                                  "Control", "Control", "Fan", "Fan", "Fan", "Compressor", "Fan"))
    
    LocateFans <- which(Test$Status == "Fan", arr.ind = T)
    LocateComp <- which(Test$Status == "Compressor", arr.ind = T)
    LocateControl <- which(Test$Status == "Control", arr.ind = T)
    
    
    for(i in LocateControl) {
      NextCompressor <- LocateComp[LocateComp > i]
      Fans <- which(between(LocateFans, i, min(NextCompressor)))
      Test$Status[LocateFans[Fans]] <- "Compressor"
    }