Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 计算两个月重叠的一周内每个月的天数_R - Fatal编程技术网

R 计算两个月重叠的一周内每个月的天数

R 计算两个月重叠的一周内每个月的天数,r,R,如果我有一系列的周,有没有人知道一种方法,可以使用R来计算给定周内每个月的天数 如果我有一个序列: week commencing "2015-04-22" "2015-04-29" "2015-05-06" 如何创建以下内容: week commencing Days in starting month Days overlapping "2015-04-22" 7 0 "2015-04-29" 3

如果我有一系列的周,有没有人知道一种方法,可以使用R来计算给定周内每个月的天数

如果我有一个序列:

week commencing
"2015-04-22" 
"2015-04-29" 
"2015-05-06"
如何创建以下内容:

week commencing   Days in starting month   Days overlapping
"2015-04-22"       7                         0
"2015-04-29"       3                         4
"2015-05-06"       7                         0

我知道lubridate软件包有许多有用的相关功能,但不知道如何应用它们来获得上述结果

我想,使用日期序列的
POSIXlt
版本将允许对每个日期分别考虑“月”和“周”。@BondedDust是的,您可以使用lubridate包中的月(对象)或周(对象)轻松添加一个月或一周的列。
dates<-as.Date(c("2015-04-22","2015-04-29" ,"2015-05-06"))
f<-Vectorize(function(d){
  sum(as.POSIXlt(d+0:6)$mon==as.POSIXlt(d)$mon)  
})
> data.frame(Start=dates,ThisMonth=f(dates),NextMonth=7-f(dates))
       Start ThisMonth NextMonth
1 2015-04-22         7         0
2 2015-04-29         2         5
3 2015-05-06         7         0