[英]How can I calculate the number of nights PER MONTH between two dates in R even if they are across two months?
[英]Apportion number of days per month between two dates in R code?
我對 R 編程很陌生,正在嘗試確定兩個日期之間每月分配的天數。
我有一個具有以下結構的數據集: from_date to_date 數量
示例數據:
2019-06-15 2019-09-10 55
2019-07-11 2019-10-05 17
我想調用一個返回數據集/向量的函數? 包含 3 個值,因為 from_date 和 to_date 之間的最大差異為 3 個月。
我曾嘗試使用lubridate::floor_date()
從to_date
向后工作
不確定您是否正在尋找如下結果:
df$quantity <- with(df,as.Date(to_date)-as.Date(from_date))
或者
df$quantity <- apply(df, 1, function(v) diff(as.Date(v)))
屈服
> df
from_date to_date quantity
1 2019-06-15 2019-09-10 87
2 2019-07-11 2019-10-05 86
數據
df <- structure(list(from_date = structure(1:2, .Label = c("2019-06-15",
"2019-07-11"), class = "factor"), to_date = structure(1:2, .Label = c("2019-09-10",
"2019-10-05"), class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.