Scott Scott - 3 months ago 11
R Question

Trying to trim a dataset based on first time a threshold is passed

I have a dataset that basically goes

p t
0 35.6
0 34
0.08 33.9
0 33.9
0.72 33.9
0.82 33.9
0.78 33.9
0.78 33.9
0.02 33.9
0.81 33.9
0.81 33.9
0.81 33.9
0.77 28.6
0.71 21
0.16 20.2
0 33.9


And want to trim the dataset to entries between when p first rises to above .1 and when t first drops below the value it has when p trips the start threshold.

The syntax I've tried is

dataset$delete <- 0
dataset$p <- as.numeric(as.character(dataset$p))
for (i in seq(along=dataset$p)) {if (dataset$p[i] < .1) {dataset$delete <- 1} else {break("done")}}


and I can't figure out why it doesn't want to work, specifically why I get the report that the loop has stopped but then go in and find that delete has been set to 1 for all observations.

I feel like this comes down to me having forgotten how loops work in R, but I can't place the issue. Any tips?

Answer
dat <- read.table(head=TRUE, text = "p       t
0       35.6
0       34
0.08    33.9
0       33.9
0.72    33.9
0.82    33.9
0.78    33.9
0.78    33.9
0.02    33.9
0.81    33.9
0.81    33.9
0.81    33.9
0.77    28.6
0.71    21
0.16    20.2
0       33.9")


## i0: row index when p first rises to above .1
thresh.p <- 0.1
i0 <- min(which(dat$p > thresh.p))

## thresh.t: value of t when p trips the start threshold
thresh.t <- dat$t[i0]
## trick: reset values of t to thresh.t for i<=i0,
## so that the first t to drop below thresh.t has row index larger than i0
dat2 <- dat
dat2$t[1:i0] <- thresh.t
i1 <- min(which(dat2$t < thresh.t))

dat[i0:i1, ]
Comments