vector - R change value to another -
i have following vector:
a <- c(true, true, false, false, true, false, false, false, true, na, false)
i change false
true
when there @ n
false
values next each other. otherwise, values should not changed. example, when n = 2
, want obtain:
c(true, true, true, true, true, false, false, false, true, na, true).
what best solution of problem?
rle()
and inverse.rle()
can help:
a <- c(true, true, false, false, true, false, false, false, true, na, false) r <- rle(a) r$values[r$values==false & r$lengths<=2] <- true inverse.rle(r) # > inverse.rle(r) # [1] true true true true true false false false true na true
Comments
Post a Comment