r - Variable definition with mutate that depends on its value in the previous row -
i have data in following format:
time click interaction 1 407 false true 2 408 true true 3 409 false false 4 410 false false 5 411 false false 6 412 false false 7 413 false false 8 414 false false 9 415 false false 10 416 false false 11 417 false false 12 418 false false 13 419 false false 14 420 false false 15 421 false false 16 422 false false 17 423 false false 18 424 false false 19 425 false false 20 426 false false 21 427 false false 22 428 false false 23 429 false false 24 430 false false 25 431 false false 26 432 false false 27 433 false false 28 434 false false 29 435 false true 30 436 false false
it represents how user interacts application every second (clicks, , other interaction events typing, scrolling, etc., , interaction
true when there's interaction, click or otherwise). i'd compute new variable true in span there's no interaction after clicking until start interacting again.
so new variable, want true if there was:
- a click in last second , no interaction (click or otherwise) in current second, or
- no interaction after click in last second, , there's still no interaction in current second.
i tried dplyr:
activity %>% mutate( nothing.after.click = (lag(click) == true & interaction == false) | (lag(nothing.after.click) == true & interaction == false) )
but unfortunately doesn't work (it says "error: object 'nothing.after.click' not found"). how can this? if isn't possible dplyr, welcome use of else.
this output i'd like:
time click interaction nothing.after.click 1 407 false true false 2 408 true true false 3 409 false false true 4 410 false false true 5 411 false false true 6 412 false false true 7 413 false false true 8 414 false false true 9 415 false false true 10 416 false false true 11 417 false false true 12 418 false false true 13 419 false false true 14 420 false false true 15 421 false false true 16 422 false false true 17 423 false false true 18 424 false false true 19 425 false false true 20 426 false false true 21 427 false false true 22 428 false false true 23 429 false false true 24 430 false false true 25 431 false false true 26 432 false false true 27 433 false false true 28 434 false false true 29 435 false true false 30 436 false false false
ultimately, goal filter these rows nothing.after.click
true, if there's way think problem i'd welcome too.
you can't reference variable in initial definition. can do in multiple passes.
when @ conditions:
nothing.after.click = (lag(click) == true & interaction == false) | (lag(nothing.after.click) == true & interaction == false)
i see interaction == false
in both possibilities. so, if interaction
true
, nothing.after.click
(from here on out nac
) false. otherwise, i'm not sure yet i'll set na
. that's first pass:
dat %>% mutate(nac = ifelse(interaction, false, na))
we've taken care of interaction == false
part, next pass lag(click) == true
part of or clause. na
, therefore undecided yet, true if lag(click)
true, otherwise we'll leave untouched. (== true
redundant, left out.)
dat %>% mutate(nac = ifelse(interaction, false, na), nac = ifelse(lag(click) & is.na(nac), true, nac))
for last pass lag(nac)
part, still undefined set previous defined value. job zoo:na.locf
(locf stands "last observation carried forward"):
library(zoo) dat %>% mutate(nac = ifelse(interaction, false, na), nac = ifelse(lag(click) & is.na(nac), true, nac), nac = na.locf(nac)) # time click interaction nac # 1 407 false true false # 2 408 true true false # 3 409 false false true # 4 410 false false true # 5 411 false false true # 6 412 false false true # 7 413 false false true # 8 414 false false true # 9 415 false false true # 10 416 false false true # 11 417 false false true # 12 418 false false true # 13 419 false false true # 14 420 false false true # 15 421 false false true # 16 422 false false true # 17 423 false false true # 18 424 false false true # 19 425 false false true # 20 426 false false true # 21 427 false false true # 22 428 false false true # 23 429 false false true # 24 430 false false true # 25 431 false false true # 26 432 false false true # 27 433 false false true # 28 434 false false true # 29 435 false true false # 30 436 false false false
Comments
Post a Comment