I have data.frame which looks like this:
v1 <- c(1:10)
v2 <- c(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)
dfb <- data.frame(v1, v2)
> dfb
v1 v2
1 1 FALSE
2 2 FALSE
3 3 TRUE
4 4 FALSE
5 5 FALSE
6 6 FALSE
7 7 TRUE
8 8 FALSE
9 9 FALSE
10 10 FALSE
I need those operations:
- split data.frame into intervals according to
V2
if isTRUE
- rows where
V2
isTRUE
will be last interval element - if the last element is not
TRUE
it will be treated as if is (this can be easily achieved by addingTRUE
to last vector position) - print
V1
as first and last element from created intervals
after this operations my results should look like this:
> df_final
Vx Vy
1 3
4 7
8 10
I've tried cumsum
on v2
vector but TRUE
values are treated as first interval element not last
> split(v2, cumsum(v2==TRUE))
$`0`
[1] FALSE FALSE
$`1`
[1] TRUE FALSE FALSE FALSE
$`2`
[1] TRUE FALSE FALSE FALSE