Vectoring code is required in R. Here is an example:
WeekdayList2 <- function(n) {
Today <- as.Date(Sys.time())
dayz <- seq(Today, Today - 2 * n, "-1 days")
dayz <- dayz[!(weekdays(dayz) %in% c("Saturday", "Sunday"))]
dayz <- dayz[seq_len(n)]
return(dayz)
}
identical(WeekdayList2(1000), WeekdayList(1000))
system.time(WeekdayList2(10000))
system.time(WeekdayList(10000))
[1] TRUE
user system elapsed
0 0 0
user system elapsed
4.90 0.00 4.91
As you can see, although my function creates a vector twice its size (and then deletes the output), it is much faster than using the for loop. My computer can't even run your function using n = 100000(not what you would need so many days ago), but WeekdayList2 runs it almost instantly.
, , , , , .