Home > other >  Withdraw second response when a first response is doubled
Withdraw second response when a first response is doubled

Time:05-22

I have a vector consisting in a digital suite of values in ms :

vect = c(450, 649, 1900, 2200, 3050, 3090)

I would like the timelag between N and N 1 be shorter than 200 ms and if it were not the case, transform N 1 into NA. Here for instance, it would transform value 649 ms and value 3090 ms into NAs. How can I do that ?

Thanks a lot for your help

CodePudding user response:

look these functions up: Ifelse() and lead() or lag(). Or if_else() to force consistent result types.

CodePudding user response:

I'm a bit confused by your description but I think this is what you mean:

vect = c(450, 649, 1900, 2200, 3050, 3090)

vect_diff <- vect |> 
  diff() |>
  {\(x) c(0, x)}()

vect_replaced <- ifelse(
  vect_diff < 200,
  vect,
  NA_integer_
)
  • Related