Scott
Scott

Reputation: 27

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?

Upvotes: 0

Views: 40

Answers (2)

shayaa
shayaa

Reputation: 2797

A rather short, yet idiomatic dplyr solution would be

 library(dplyr)
 df %>% filter(p>.1) %>% filter(t >= t[1])

Gives as expected

     p    t
1 0.72 33.9
2 0.82 33.9
3 0.78 33.9
4 0.78 33.9
5 0.81 33.9
6 0.81 33.9
7 0.81 33.9

Upvotes: 1

renato vitolo
renato vitolo

Reputation: 1754

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, ]

Upvotes: 1

Related Questions