Paul Rougieux - 1 year ago 47

R Question

To parse a text file, I want to extract all numeric lines after a certain character string appears. I have managed to extract the text. Now selecting only numeric lines that appear after this text boils down to this little problem. From a vector of 0 and 1, I want to keep only the ones before the first 0.

`a <- c(1,0,1)`

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

The function f should return:

`f(a)`

c(1,0,0)

f(b)

c(1,1,1,1,0,0,0,0,0,0,0)

I tried using

`which.min(a)`

[1] 2

which.min(b)

[1] 5

Or

`cumsum(!a)==0`

[1] TRUE FALSE FALSE

cumsum(!b)==0

[1] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

I guess there is a simple solution but I'm probably too sleepy this morning.

Answer Source

There is a `cummin`

function

```
cummin(a)
#[1] 1 0 0
cummin(b)
#[1] 1 1 1 1 0 0 0 0 0 0 0
```