Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting only the 0s and the first 1 from a sequence of many 0s and few 1s in R?

Tags:

r

sequence

I have a sequence of 0s and 1s in this manner:

xx <- c(0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 
                    0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1)

And I want to select the 0s and the first 1s.

The results should be:

ans <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1)

What's the fastest way? in R

like image 953
xav Avatar asked Sep 20 '13 15:09

xav


2 Answers

Use rle() to extract the run lengths and values, do some minor surgery, and then put the run-length encoded vector "back together" using inverse.rle().

rr <- rle(xx)
rr$lengths[rr$values==1] <- 1
inverse.rle(rr)
#  [1] 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1
like image 115
Josh O'Brien Avatar answered Jan 03 '23 23:01

Josh O'Brien


Here's one way:

idx <- which(xx == 1)
pos <- which(diff(c(xx[1], idx)) == 1)
xx[-idx[pos]] # following Frank's suggestion
# [1] 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1
like image 23
Arun Avatar answered Jan 04 '23 00:01

Arun