2016-08-07 1 views
0

を渡された最初の時間に基づいてデータセットをトリミングしようとすると、私は基本的にしきい値を

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 

を行くデータセットを持っており、pは最初0.1以上とするときトンに上昇するとの間のエントリにデータセットをトリミングしますpが開始スレッシュホールドをトリップしたときに最初に値を下回ります。

私が試した構文は

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")}} 

であり、それが動作するように望んでいない理由を私は理解できない、特に私はループが停止したことをレポートを取得する理由が、その後で行くとすべての観測に対して削除が1に設定されていることを確認します。

私はRにどのようにループが働いているのか忘れてしまったような気がしますが、問題は解決できません。任意のヒント? (分( -

答えて

1
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, ] 
+0

感謝を与えるだろうこれは(dat2 $ t Scott

+0

すべてがうまく終わりです –

1

かなり短い、まだ慣用dplyrソリューションは、予想通り、私は最後から二番目の行は「I1 <すべきだと思いますが、

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

は、

 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 
+0

"0.02 33.9" – Scott

関連する問題