Okay, your last comment gets at some of the confusion:
We have two signal vector, the old one (11,11,11,11,11) and the new one we are creating: signal = ifelse...etc.
In R, you can't reference a new variable as it is being created, you have to finish creating it first.
That said you still haven't explained, in words, what you want to do so it's very difficult to try to correct your code. I understand exactly what your code does, and why---but it's very difficult to know what you actually want since you haven't explained your logic. (This probably explains the downvotes on your question.) So this is my best guess.
## The set-up
signal = c(11, 11, 11, 11, 11)
sig1 = c(1, 2, 3, 4, 5)
sig2 = c(6, 7, 8, 9, 10)
## Let's get a temp variable, the thing we want to lag
## (again, this is a guess)
(sig.temp = ifelse(sig1 < 3, 0, signal))
# [1] 0 0 11 11 11
(new.signal = ifelse(sig1 < 3, 0, ifelse(sig2 > 8, 0.2, lag(sig.temp))))
[1] 0.0 0.0 0.0 0.2 0.2
Edits:
# Another way, this time doing both comparisons before the lag
sig.temp2 = ifelse(sig1 < 3, 0, ifelse(sig2 > 8, 0.2, signal))
new.signal = ifelse(sig1 < 3 | sig2 > 8, sig.temp2, lag(sig.temp2))
# [1] 0.0 0.0 0.0 0.2 0.2
The difference between R and Excel in this is that Excel will do things one-at-a-time, and auto-update based on changes. R will never auto-update. For example, in R
x = 1
y = x + 1
# y is 2
x = 5
y
# [1] 2
# y is still 2
However, in Excel, if you set B1 = A1 + 1, then that relationship will be maintained. Because R doesn't auto-update, and R doesn't like to do things one-at-a-time (it creates a vector all at once, not one row at a time), you need a temp variable.
More edits
Okay, looking more carefully at your spreadsheet, column D isn't used at all. Just like the c(11, 11, 11, ...)
in your original question wasn't used at all. The only columns that matter are sig1
and `sig2, that is columns E and F. Here is the relevant data from Excel, rows 14-36:
col_e = c(14.286, 13.333, 12.5, 11.765, 8.333, 5.263, 7.692, 7.5, -4.762,
-2.326, -7.5, -4.762, 2.703, -7.5, 2.632, 7.027, 0, -1.768, -1.026,
-4.37, -3.109, 2.043, -0.588) / 100
col_f = c(6.67, 6.25, 5.88, 5.56, 2.63, 2.56, 5, 2.38, -6.98, 5, -11.9,
8.11, -5, -2.63, 5.41, 1.54, -1.52, -0.26, -0.77, -3.63, 0.54,
1.5, -2.05) / 100
Along with your desired result:
desired_result = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L,
1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L)
Now let's code up your logic. For the exception case, we'll fill in a missing value:
col_g = ifelse(col_e < -.03, 0, ifelse(col_f > 0.04, 1, NA))
We then want to fill in the missing values (NA
s) with the previous non-missing value. This is done nicely with zoo::na.locf()
(stands for Last Observation Carried Forward):
library(zoo)
col_g = na.locf(col_g)
Does it match Excel?
all(na.locf(col_g) == desired_result)
# [1] TRUE
Yes.
If you want to do this in one line, you can nest the statements:
col_g = na.locf(ifelse(col_e < -.03, 0, ifelse(col_f > 0.04, 1, NA)))
Now that you gave your full code...
I called your output column "desired" in Excel, and read your data into R. Works just fine, all 3367 rows:
dat = read.table("clipboard", header = T)
result = zoo::na.locf(ifelse(dat$lambda < -8, 0, ifelse(dat$omega > 6, 1, NA)))
all(result == dat$desired)
# [1] TRUE