按星期,月份,季度和年份计算date之间的差异

我有两个date让我们说14.01.2013和26.03.2014。

我希望以星期(?),月(在例子14),季度(4)和年份(1)的方式来区分这两个date。

你知道这个最好的方法吗?

那这个呢:

 # get difference between dates `"01.12.2013"` and `"31.12.2013"` # weeks difftime(strptime("26.03.2014", format = "%d.%m.%Y"), strptime("14.01.2013", format = "%d.%m.%Y"),units="weeks") Time difference of 62.28571 weeks # months (as.yearmon(strptime("26.03.2014", format = "%d.%m.%Y"))- as.yearmon(strptime("14.01.2013", format = "%d.%m.%Y")))*12 [1] 14 # quarters (as.yearqtr(strptime("26.03.2014", format = "%d.%m.%Y"))- as.yearqtr(strptime("14.01.2013", format = "%d.%m.%Y")))*4 [1] 4 # years year(strptime("26.03.2014", format = "%d.%m.%Y"))- year(strptime("14.01.2013", format = "%d.%m.%Y")) [1] 1 

as.yearmon()as.yearqtr()在包zooyear()在包lubridate 。 你怎么看?

所有现有的答案都是不完善的(IMO),要么对期望的产出做出假设,要么不为期望的产出提供灵活性。

根据OP的例子和OP的预期的答案,我认为这些就是你正在寻找的答案(加上一些额外的例子,使其易于推断)。

(这只需要基地R,不需要动物园或lubridate)

转换为date时间对象

 date_strings = c("14.01.2013", "26.03.2014") datetimes = strptime(date_strings, format = "%d.%m.%Y") # convert to datetime objects 

天差异

您可以在几天内使用差异来获得我们稍后的答案

 diff_in_days = difftime(datetimes[2], datetimes[1], units = "days") # days diff_in_days #Time difference of 435.9583 days 

周中的差异

星期差是difftime()units = "weeks"difftime()

 diff_in_weeks = difftime(datetimes[2], datetimes[1], units = "weeks") # weeks diff_in_weeks #Time difference of 62.27976 weeks 

请注意,这与将diff_in_days除以7(一周7天)

 as.double(diff_in_days)/7 #[1] 62.27976 

年差

有了类似的逻辑,我们可以从diff_in_days得到多年

 diff_in_years = as.double(diff_in_days)/365 # absolute years diff_in_years #[1] 1.194406 

你似乎期望差不多是“1”,所以我假设你只是想计算绝对的日历年或什么,你可以轻松地使用floor()

 # get desired output, given your definition of 'years' floor(diff_in_years) #[1] 1 

四舍五入

 # get desired output for quarters, given your definition of 'quarters' floor(diff_in_years * 4) #[1] 4 

几个月的差异

可以将此计算为diff_years的转换

 # months, defined as absolute calendar months (this might be what you want, given your question details) months_diff = diff_in_years*12 floor(month_diff) #[1] 14 

我知道这个问题已经很老了,但鉴于我刚才还要解决这个问题,我想我会加上我的答案。 希望能帮助到你。

几周difftime ,你可以使用函数difftime

 date1 <- strptime("14.01.2013", format="%d.%m.%Y") date2 <- strptime("26.03.2014", format="%d.%m.%Y") difftime(date2,date1,units="weeks") Time difference of 62.28571 weeks 

但是difftime不能在数周内持续工作。
以下是使用cut.POSIXt进行处理的非常不理想的解决scheme,但您可以解决这个问题:

 seq1 <- seq(date1,date2, by="days") nlevels(cut(seq1,"months")) 15 nlevels(cut(seq1,"quarters")) 5 nlevels(cut(seq1,"years")) 2 

然而,这是您的时间间隔所涵盖的月数,季度或年数,而不是以月,季,年表示的时间间隔(因为这些持续时间不是固定的)。 考虑到你对@SvenHohenstein的回答,我会认为你可以使用nlevels(cut(seq1,"months")) - 1来实现你的目的。

我刚刚写了这个问题,然后在这里偶然发现。

 library(lubridate) #' Calculate age #' #' By default, calculates the typical "age in years", with a #' \code{floor} applied so that you are, eg, 5 years old from #' 5th birthday through the day before your 6th birthday. Set #' \code{floor = FALSE} to return decimal ages, and change \code{units} #' for units other than years. #' @param dob date-of-birth, the day to start calculating age. #' @param age.day the date on which age is to be calculated. #' @param units unit to measure age in. Defaults to \code{"years"}. Passed to \link{\code{duration}}. #' @param floor boolean for whether or not to floor the result. Defaults to \code{TRUE}. #' @return Age in \code{units}. Will be an integer if \code{floor = TRUE}. #' @examples #' my.dob <- as.Date('1983-10-20') #' age(my.dob) #' age(my.dob, units = "minutes") #' age(my.dob, floor = FALSE) age <- function(dob, age.day = today(), units = "years", floor = TRUE) { calc.age = interval(dob, age.day) / duration(num = 1, units = units) if (floor) return(as.integer(floor(calc.age))) return(calc.age) } 

用法示例:

 my.dob <- as.Date('1983-10-20') age(my.dob) # [1] 31 age(my.dob, floor = FALSE) # [1] 31.15616 age(my.dob, units = "minutes") # [1] 16375680 age(seq(my.dob, length.out = 6, by = "years")) # [1] 31 30 29 28 27 26 

这是一个解决scheme:

 dates <- c("14.01.2013", "26.03.2014") # Date format: dates2 <- strptime(dates, format = "%d.%m.%Y") dif <- diff(as.numeric(dates2)) # difference in seconds dif/(60 * 60 * 24 * 7) # weeks [1] 62.28571 dif/(60 * 60 * 24 * 30) # months [1] 14.53333 dif/(60 * 60 * 24 * 30 * 3) # quartes [1] 4.844444 dif/(60 * 60 * 24 * 365) # years [1] 1.194521 

尝试一个月的解决scheme

 StartDate <- strptime("14 January 2013", "%d %B %Y") EventDates <- strptime(c("26 March 2014"), "%d %B %Y") difftime(EventDates, StartDate)