extracting first and last positions in a dataset

Viewed 62

I have this dataset that I'm trying to transform to get the "from" and "to" positions within a particular grouping of data points that pass a test.

Here's how the data looks:

pos <- seq(from = 10, to = 100, by = 10)
test <- c(1, 1, 1, 0, 0, 0, 1, 1, 1, 0)
df <- data.frame(pos, test)

So you can see that positions 10, 20, and 30, as well as 70, 80, and 90 pass the test (b/c test = 1) but the rest of the points don't. The answer I'm looking for would be a data frame that looks something like the "answer" data frame in the code below:

peaknum <- c(1, 2)
from <- c(10, 70)
to <- c(30, 90)
answer <- data.frame(peaknum, from, to)

Any suggestions as to how I can transform the dataset? I'm stumped.

Thanks, Steve

2 Answers
Related