我写了这个我一直用的功能:
# Give the previous day, or Friday if the previous day is Saturday or Sunday.
previous_business_date_if_weekend = function(my_date) {
if (length(my_date) == 1) {
if (weekdays(my_date) == "Sunday") { my_date = lubridate::as_date(my_date) - 2 }
if (weekdays(my_date) == "Saturday") { my_date = lubridate::as_date(my_date) - 1 }
return(lubridate::as_date(my_date))
} else if (length(my_date) > 1) {
my_date = lubridate::as_date(sapply(my_date, previous_business_date_if_weekend))
return(my_date)
}
}
Run Code Online (Sandbox Code Playgroud)
当我将其应用于具有数千行的数据帧的日期列时出现问题.这太慢了. 有什么想法为什么?