替换R中的行值序列

xzabzqsa  于 2023-02-26  发布在  其他
关注(0)|答案(3)|浏览(137)

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.

os8fio9y

os8fio9y1#

我认为使用游程编码可以稳健地处理这个问题。

quux <- structure(list(Status = c("Control", "Control", "Fan", "Fan", "Fan", "Compressor", "Compressor")), class = "data.frame", row.names = c(NA, -7L))
r <- rle(quux$Status)
r
# Run Length Encoding
#   lengths: int [1:3] 2 3 2
#   values : chr [1:3] "Control" "Fan" "Compressor"
ind <- r$values == "Fan" &
  c(FALSE, r$values[-length(r$values)] == "Control") &
  c(r$values[-1] == "Compressor", FALSE)
ind
# [1] FALSE  TRUE FALSE
r$values[ind] <- "Compressor"
quux$newStatus <- inverse.rle(r)
quux
#       Status  newStatus
# 1    Control    Control
# 2    Control    Control
# 3        Fan Compressor
# 4        Fan Compressor
# 5        Fan Compressor
# 6 Compressor Compressor
# 7 Compressor Compressor
zdwk9cvp

zdwk9cvp2#

只是为了好玩,我认为这工程以及只是循环压缩机的位置,寻找元素之间的风扇和更换。伟大的答案以上。

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"
}
gudnpqoy

gudnpqoy3#

或者与Tidyverse:

library(dplyr)
library(tidyr)
df <- tibble(Status = c(rep("Control",2),
                      rep("Fan",3),
                      rep("Compressor",2)))
df
#> # A tibble: 7 × 1
#>   Status    
#>   <chr>     
#> 1 Control   
#> 2 Control   
#> 3 Fan       
#> 4 Fan       
#> 5 Fan       
#> 6 Compressor
#> 7 Compressor

df %>% 
  nest(.by = Status) %>% 
  filter(!(Status == "Fan" & 
         lag(Status) == "Control" &  
         lead(Status) == "Compressor")) %>% 
  unnest(data)
#> # A tibble: 4 × 1
#>   Status    
#>   <chr>     
#> 1 Control   
#> 2 Control   
#> 3 Compressor
#> 4 Compressor

创建于2023年2月24日,使用reprex v2.0.2

相关问题