mardi 29 septembre 2020

Different output when running a line inside vs. outside an ifelse() statement

I am trying to run a simple command but no idea why the output is different when running it inside vs outside an ifelse() function. The function condition evaluates to FALSE, so the output should be the exact same.

However, when running it alone, the output is 0 0 1 1 0 1 0 1 NA (as desired) but from the ifelse() function, the output is 0 (not desired).

library(dplyr)
library(zoo)

x <- c(0, 1, 1, 0, 0, 1, 0, 1, 0)
del <- 2

dplyr::lead(zoo::rollsum(x, del - 1, fill = NA, align = "left")) == 0
[1] FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE    NA

ifelse(del == 1, -1, dplyr::lead(zoo::rollsum(x, del - 1, fill = NA, align = "left")) == 0)
[1] FALSE

I would appreciate any help on why this is happening! Never seen something like this before. The outcomes of the ifelse() have different lengths depending on whether the condition is evaluated as true or false, but I don't see why this would cause a truncation of the longer output.

Aucun commentaire:

Enregistrer un commentaire