88

と の 2 つの日付が14.01.2013あり26.03.2014ます。

これらの 2 つの日付の差を、週 (?)、月 (例では 14)、四半期 (4)、年 (1) で取得したいと考えています。

これを取得する最良の方法を知っていますか?

4

9 に答える 9

80

これはどうですか:

# 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。どう思いますか?

于 2013-01-22T09:28:59.407 に答える
61

既存の回答はすべて不完全 (IMO) であり、目的の出力について仮定を行うか、目的の出力に柔軟性を提供しません。

OPの例とOPの予想される回答に基づいて、これらはあなたが探している答えだと思います(さらに、簡単に推定できるいくつかの追加の例)。

(これには base R のみが必要で、zoo や lubridate は必要ありません)

日時オブジェクトに変換

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

週の差

週の差は特殊なケース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

この質問が古いことは知っていますが、今でもこの問題を解決しなければならなかったので、回答を追加すると思いました。それが役に立てば幸い。

于 2015-04-22T00:48:45.513 に答える
14

数週間、 function を使用できます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ですが、回避できます。

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と、達成しようとしていることに使用できると思います。

于 2013-01-22T09:20:16.137 に答える
14

別の質問のためにこれを書いたところ、ここでつまずきました。

library(lubridate)

#' Calculate age
#' 
#' By default, calculates the typical "age in years", with a
#' \code{floor} applied so that you are, e.g., 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
于 2014-12-08T18:26:39.707 に答える
6

解決策は次のとおりです。

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
于 2013-01-22T08:51:52.763 に答える
2

これを数か月試してみてください

StartDate <- strptime("14 January 2013", "%d %B %Y") 
EventDates <- strptime(c("26 March 2014"), "%d %B %Y") 
difftime(EventDates, StartDate) 
于 2013-01-22T08:50:50.460 に答える