If you want to store which rows are TRUE preallocate an empty vector and bind it in the loop.
If you really have TRUE
and FALSE
as strings, do the following
Holidays <- c("TRUE", "TRUE", "FALSE", "TRUE")
store = c()
for (i in 1:length(Holidays)){
if (Holidays[i] == "TRUE") # here you want to see whether the element inside Holiday is equals "TRUE"
store = c(store, i) # store vector you give you which rows are "TRUE"
}
However, if you have actual logical entries in Holidays
, then the correct syntax should be
Holidays <- c(TRUE, TRUE, FALSE, TRUE)
store = c()
for (i in 1:length(Holidays)){
if (Holidays[i] == TRUE)
store = c(store, i)
}
However, as @Badger mentioned, there is no need to loop here. You can easily get the indices using which
store = which(Holidays %in% TRUE)