0

i want to have a past-2-days condition in while statement, so i write in this form:

while (xxx$mdate >= Sys.Date()-2 && xxx$mdate < Sys.Date()) {}

*mdate in date and time.

when i use single condition without AND, it works but it doesn't work with AND.

Eric
  • 11
  • 3
  • 1
    Could you supply a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) ? – Waldi Mar 15 '23 at 09:06
  • `while` expects a single logical value. Try `&` instead of `&&` – Sotos Mar 15 '23 at 09:23

1 Answers1

0

You should contemplate the difference between the conventional vectorised and & the early stopping variant &&

(a1 <- c(FALSE,TRUE,FALSE))
(b2 <- rep(TRUE,3))

a1 & b2 
a1 && b2

Also while loops, are useful in R in a rare set of circumstances, its very unlikely that your code wouldn't be more elegant and robust to errors if you made use of R's vectorisation, and/or iteration via apply family or tidyverse purr map functions, as its been said; computers will need to loop, but why should you have to worry about the details ?

more links :

1)https://r4ds.had.co.nz/iteration.html#for-loops-vs.-functionals

2)http://adv-r.had.co.nz/Functional-programming.html

Nir Graham
  • 2,567
  • 2
  • 6
  • 10