mardi 26 octobre 2021

R: ifelse() returns TRUE/FALSE instead of value

for me something weird is happening in my R-Script... Im using following statement:

dt_DAY[, tmp1 := ifelse(KT_fd == 1,sequence(rle(as.character(KT_fd))$lengths),0), by=id]
dt_DAY[, tmp2 := ifelse(tmp1== 0,1,0),by=id]
dt_DAY[, tmp2 := shift(tmp2, 1),by=id]
dt_DAY[, KT_fsdi0 := tmp1 * tmp2]

tmp1 and tmp2 just return TRUE/FALSE and not 0 or the result of the sequence function. Do you have any idea?

Edit:

This is kind of how it should look like: [1]: https://i.stack.imgur.com/gZSDV.png The idea is to find out how many days in a row with KT_fd == 1.

Acutally I can make parts of the code. This is how I created tmp1_expected:

dt_DAY[, tmp1_expected := sequence(rle(as.character(KT_dd))$lengths) by=id] dt_DAY$tmp1_expected <- dt_DAY$tmp1_expected*dt_DAY$KT_dd

Aucun commentaire:

Enregistrer un commentaire