0

I have a dataframe of values (3 years in 10 minutes steps) and I want that if the barrier is higher than 900 that x gets NA for that time and that the previous 360 and following 360 values also get NA. x and barrier contain NAs.

My dataframe consist out of: enter image description here

  • Time df$Time<- as.POSIXct(df$Time,format="%Y-%m-%d %H:%M",tz="GMT") [10 minute steps]
  • x my value what I want to be sorted out by the barrier

  • barrier if the barrier gets over 900 my x value should get NA That works with df$x <- ifelse(df$barrier > 900, NA, df$x) here you can see. enter image description here

but I want that also the previous 6hours = 360 values should get NA as well as the following 6hours =360 values of x.

You can see on the picture that if the barrier (pink line) is over 900 the previous and following values are also affected (jumps), hence I want that the x values before and after get also NA . enter image description here

My problem is I do not know how I can remove the previous and following values of x

Nad
  • 21
  • 3
  • Please review [how to ask](https://stackoverflow.com/help/how-to-ask) questions and then provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), including sample data. We don't know anything about your data, which makes the code snippet you give completely useless. Always provide (1) sample data, (2) the code you've tried, and (3) the expected output based on your sample data. It's also not clear what you're actually asking. Did the command work? If not, why not. Were there errors? We need details! – Maurits Evers Sep 06 '18 at 12:47

1 Answers1

0

You can probably find more elegant way but this works:

indeks <- df$barrier > 900
for(i in 1:360){
  if(indeks[i]==T){
    df$x[1:(i+360)] <- NA
  }
}
for(i in (360+1):(length(indeks)-361)){
  if(indeks[i]==T){
    df$x[(i-360):(i+360)] <- NA
  }
}
for(i in (length(indeks)-360):length(indeks)){
  if(indeks[i]==T){
    df$x[(i-360):lentgh(index)] <- NA
  }
}
JacobJacox
  • 917
  • 5
  • 14