How to extract all the lines between the start signal and the end signal?

I have the following df, and I would like to extract all the lines based on the following start and end signals.

Trigger signal: when the state changes from 1 to 0 End signal: when the state changes from 0 to -1.

df <- data.frame(time = rep(1:14), status = c(0,1,1,0,0,0,-1,0,1,0,0,0,-1,0))

   time status
1     1      0
2     2      1
3     3      1
4     4      0
5     5      0
6     6      0
7     7     -1
8     8      0
9     9      1
10   10      0
11   11      0
12   12      0
13   13     -1
14   14      0

A wish:

   time status    
4     4      0
5     5      0
6     6      0
10   10      0
11   11      0
12   12      0
+4
source share
4 answers

A solution using a package is possible here data.table. I basically group by indicators first status == 1, and then checking each group, if there is also status == -1, if so, I add the group from the second incident until the incident -1minus 1

library(data.table)
setDT(df)[, indx := cumsum(status == 1)]
df[, if(any(status == -1)) .SD[2:(which(status == -1) - 1)], by = indx]
#    indx time status
# 1:    2    4      0
# 2:    2    5      0
# 3:    2    6      0
# 4:    3   10      0
# 5:    3   11      0
# 6:    3   12      0 
+6
source

, (start-end) . (cumsum(start)-cumsum(end)>1) - , 2, , ; 14 .

require(dplyr)

df %>% mutate(start=(status==1), end=(status==-1)) %>%
       filter(!start & !end & (cumsum(start)-cumsum(end)>1) ) %>%
       select(-start, -end)

#   time status
# 1    4      0
# 2    5      0
# 3    6      0
# 4   10      0
# 5   11      0
# 6   12      0
+2

, , .

keepers <- rep(FALSE, nrow(df))
flag <- FALSE
for(i in 1:(nrow(df)-1)) {
    if(df$status[i] == 1 && df$status[i+1] == 0) { 
        flag <- TRUE
        next  # keep signal index false
    }
    if(df$status[i] == -1 && df$status[i+1] == 0) {
        flag <- FALSE
        next  # keep signal index false
    }
    keepers[i] <- flag
}
keepers[nrow(df)] <- flag  # Set the last element to final flag value
newdf <- df[keepers, ]  # subset based on the T/F values determined
+1

( , ), , / ?

:

library(stringr)

df <- data.frame(time = rep(1:14), status = c(0,1,1,0,0,0,-1,0,1,0,0,0,-1,0))

dfr <- rle(df$status)

# first approach 

find_seq_str <- function() {
  str_locate_all(paste(gsub("-1", "x", dfr$values), collapse=""), "10")[[1]][,2]
}

df[as.vector(sapply(find_seq_str(), 
  function(n) {
    i <- sum(dfr$lengths[1:(n-1)])
    tail(i:(i+dfr$lengths[n]), -1)
  })),]


# second approach

find_seq_ts <- function() {
  which(apply(embed(dfr$values, 2), 1, function(x) all(x == c(0, 1))))
}

df[as.vector(sapply(find_seq_ts(), 
  function(n) {
    i <- sum(dfr$lengths[1:(n)])+1
    head(i:(i+dfr$lengths[n+1]), -1)
  })),]

status.

-1, , str_locate, , , , rle .

R, - regexpr.

.

:

  • , status .
  • , ( q).
  • , , "", data.table; -)

I wrapped up the approaches in functions since they could potentially be parameterized, but you could just simply assign a value to a variable or paste it into sapply(ugh, tho).

+1
source

Source: https://habr.com/ru/post/1609708/


All Articles