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

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -