如何计算不同月份的 R 中的 MTD
how to calculate MTD in R for a different month
任务是找出两个月的 MTD 天数,在我的案例中是流入量最高的月份(7 月)和当前月份。
因为我计划 运行 每天将语句作为脚本,所以我不想硬编码任何东西。
数据框是这样的:
SERVICE BEST MONTH TOTAL BEST MONTH MTD CURR. MONTH MTD
No of Working Days
..
..
..
对于"BEST MONTH TOTAL",我使用了以下语句:
report[1,2] <- sum(!weekdays(seq(as.Date('2019-07-01'), as.Date('2019-07-
31'), 'days')) %in% c('Sunday','Saturday'))
对于当月的天数 MTD,我使用以下方法计算的天数:
difftime(Sys.Date(),'2019-09-01',units = "days" )
这给出了输出:
Time difference of 12.22917 days
有没有办法让我只得到整数 12?
如何计算最佳月度 MTD?是否有一个函数可以帮助返回与 7 月份的 sys.date() 相同的日期来计算工作日 MTD 的数量?
即基本上我需要的是:
difftime('2019-07-13','2019-07-01', units = "days")
但不想对“2019-07-13”进行硬编码,因为我想运行将其作为脚本并希望避免每天更改日期。另外我只需要没有 "Time difference of ... days".
的整数差
要转换为天数,作为数字:
as.numeric(difftime(Sys.Date(),'2019-09-01',units = "days" ))
这是你想要的吗?
trunc(difftime(Sys.Date(),'2019-09-01',units = "days"))
#output
#Time difference of 12 days
best_month_mtd <- function(y) {
trunc(difftime(Sys.Date(),y, units = "days"))
}
#typical usage
best_month_mtd('2019-09-01')
#output
#Time difference of 12 days
那应该给你你想要的。
更新
如果您只想计算从该月的开始日期起经过的天数,使用 lubridate
的单行代码可能会很方便。这可能与实际工作日数不同。后者需要处理组织中日历关闭的日期以及国定假日。
library(lubridate)
# -------------------------------------------------------------------------
#The start date of the month can be computed using floor_date() from lubridate
#floor_date() takes a date-time object and rounds it down to the nearest
#boundary of the specified time unit.
month_start <- floor_date(Sys.Date(), "month")
month_start
#"2019-09-01"
# -------------------------------------------------------------------------
#difftime between Sys.Date() and month_start
difftime(Sys.Date(),month_start, units = "days")
#Time difference of 12 days
# -------------------------------------------------------------------------
# to print only the number of days
paste0(trunc(difftime(Sys.Date(),month_start, units = "days")))
#"12"
# -------------------------------------------------------------------------
#Using the above a one liner can be constructed follows.
paste0(trunc(difftime(Sys.Date(),floor_date(Sys.Date(), "month"), units = "days")))
任务是找出两个月的 MTD 天数,在我的案例中是流入量最高的月份(7 月)和当前月份。 因为我计划 运行 每天将语句作为脚本,所以我不想硬编码任何东西。
数据框是这样的:
SERVICE BEST MONTH TOTAL BEST MONTH MTD CURR. MONTH MTD
No of Working Days
..
..
..
对于"BEST MONTH TOTAL",我使用了以下语句:
report[1,2] <- sum(!weekdays(seq(as.Date('2019-07-01'), as.Date('2019-07-
31'), 'days')) %in% c('Sunday','Saturday'))
对于当月的天数 MTD,我使用以下方法计算的天数:
difftime(Sys.Date(),'2019-09-01',units = "days" )
这给出了输出:
Time difference of 12.22917 days
有没有办法让我只得到整数 12?
如何计算最佳月度 MTD?是否有一个函数可以帮助返回与 7 月份的 sys.date() 相同的日期来计算工作日 MTD 的数量? 即基本上我需要的是:
difftime('2019-07-13','2019-07-01', units = "days")
但不想对“2019-07-13”进行硬编码,因为我想运行将其作为脚本并希望避免每天更改日期。另外我只需要没有 "Time difference of ... days".
的整数差要转换为天数,作为数字:
as.numeric(difftime(Sys.Date(),'2019-09-01',units = "days" ))
这是你想要的吗?
trunc(difftime(Sys.Date(),'2019-09-01',units = "days"))
#output
#Time difference of 12 days
best_month_mtd <- function(y) {
trunc(difftime(Sys.Date(),y, units = "days"))
}
#typical usage
best_month_mtd('2019-09-01')
#output
#Time difference of 12 days
那应该给你你想要的。
更新
如果您只想计算从该月的开始日期起经过的天数,使用 lubridate
的单行代码可能会很方便。这可能与实际工作日数不同。后者需要处理组织中日历关闭的日期以及国定假日。
library(lubridate)
# -------------------------------------------------------------------------
#The start date of the month can be computed using floor_date() from lubridate
#floor_date() takes a date-time object and rounds it down to the nearest
#boundary of the specified time unit.
month_start <- floor_date(Sys.Date(), "month")
month_start
#"2019-09-01"
# -------------------------------------------------------------------------
#difftime between Sys.Date() and month_start
difftime(Sys.Date(),month_start, units = "days")
#Time difference of 12 days
# -------------------------------------------------------------------------
# to print only the number of days
paste0(trunc(difftime(Sys.Date(),month_start, units = "days")))
#"12"
# -------------------------------------------------------------------------
#Using the above a one liner can be constructed follows.
paste0(trunc(difftime(Sys.Date(),floor_date(Sys.Date(), "month"), units = "days")))