1
私はrenkoベースの時系列データをダウンロードしました。定期的な時系列データではないので、ts
ライブラリを使用することはできません。最終行の条件に基づいて新しい列を作成する
私は連続した肯定/否定のrenkoボックスを要約する列を作成しようとしています。私がこれまで行ってきた何
:
library(xlsx)
last <- function(arr, line_no, offset) {
line_no <- max(1, line_no - offset)
line_no <- min(nrow(arr), line_no)
arr[arr$idx == line_no,]
}
wdofut <- read.xlsx('~/Documents/tmp/WDOFUT_5R_20171219.xlsx',sheetIndex = 1,header=TRUE)
wdofut <- wdofut[ order(unclass(wdofut[,1])), ]
wdofut$idx <- 1:nrow(wdofut)
wdofut$positive <- wdofut$Abertura < wdofut$Fechamento
wdofut$lpositive <- last(wdofut,wdofut$indice,1)$positive
wdofut$negative <- wdofut$Abertura > wdofut$Fechamento
wdofut$lnegative <- last(wdofut,wdofut$indice,1)$negative
このコードを実行した後、lpositive
はTRUE
ままで、Rコンソール上のときのコールlast
機能lnegative
は、しかし、すべての行
> cols <- c(7,1,8:11)
> wdofut[c(1:15),cols]
idx Data positive lpositive negative lnegative
5454 1 2017-07-05 17:58:59 TRUE TRUE FALSE FALSE
5449 2 2017-07-06 09:00:00 TRUE TRUE FALSE FALSE
5450 3 2017-07-06 09:00:00 TRUE TRUE FALSE FALSE
5451 4 2017-07-06 09:00:00 TRUE TRUE FALSE FALSE
5452 5 2017-07-06 09:00:00 TRUE TRUE FALSE FALSE
5453 6 2017-07-06 09:00:00 TRUE TRUE FALSE FALSE
5448 7 2017-07-06 09:01:00 TRUE TRUE FALSE FALSE
5446 8 2017-07-06 09:01:59 TRUE TRUE FALSE FALSE
5447 9 2017-07-06 09:01:59 TRUE TRUE FALSE FALSE
5445 10 2017-07-06 09:06:00 FALSE TRUE TRUE FALSE
5444 11 2017-07-06 09:14:59 TRUE TRUE FALSE FALSE
5442 12 2017-07-06 09:24:00 FALSE TRUE TRUE FALSE
5443 13 2017-07-06 09:24:00 TRUE TRUE FALSE FALSE
5441 14 2017-07-06 09:36:00 FALSE TRUE TRUE FALSE
5440 15 2017-07-06 09:58:00 FALSE TRUE TRUE FALSE
>
上FALSE
まま、正解を返す
> last(wdofut,11,1)[,cols]
idx Data positive lpositive negative lnegative
5445 10 2017-07-06 09:06:00 FALSE TRUE TRUE FALSE
>
lpositiveとlnegativeの値が1行目と同じであることがわかりましたが、その理由を理解できません。
これを理解するのに手伝ってもらえますか?
「最後」とは、「前」を意味しますか? – Mako212
'last()'は、data.frameの最後の行を返します(つまり、 'wdofut [nrow(wdofut)]))。以前の値が必要だと仮定して、 'dplyr :: lag'と' mutate'を使って新しい列を作成することを考えています。 – Mako212
はい。実際には、最後の関数は指定されたオフセットだけ前の行を取得します。私がrownに頼ることはできないことに注意してください。列1にあるタイムスタンプに基づいて注文を修正する必要があるからです。 –