如何减去年份?

发布于 2024-09-11 00:38:36 字数 161 浏览 8 评论 0原文

我在 R 中有一个日期,例如:

dt = as.Date('2010/03/17')

我想从此日期减去 2 年,而不用担心闰年和此类问题,得到 as.Date('2008-03-17')

我该怎么做呢?

I have a date in R, e.g.:

dt = as.Date('2010/03/17')

I would like to subtract 2 years from this date, without worrying about leap years and such issues, getting as.Date('2008-03-17').

How would I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

慕巷 2024-09-18 00:38:36

使用润滑

library(lubridate)
ymd("2010/03/17") - years(2)

With lubridate

library(lubridate)
ymd("2010/03/17") - years(2)
仙女 2024-09-18 00:38:36

最简单的方法是将其转换为 POSIXlt 并从年份槽中减去 2。

> d <- as.POSIXlt(as.Date('2010/03/17'))
> d$year <- d$year-2
> as.Date(d)
[1] "2008-03-17"

请参阅此相关问题:如何在 R 中减去天数?

The easiest thing to do is to convert it into POSIXlt and subtract 2 from the years slot.

> d <- as.POSIXlt(as.Date('2010/03/17'))
> d$year <- d$year-2
> as.Date(d)
[1] "2008-03-17"

See this related question: How to subtract days in R?.

盗琴音 2024-09-18 00:38:36

您可以使用seq

R> dt = as.Date('2010/03/17')
R> seq(dt, length=2, by="-2 years")[2]
[1] "2008-03-17"

You could use seq:

R> dt = as.Date('2010/03/17')
R> seq(dt, length=2, by="-2 years")[2]
[1] "2008-03-17"
ˉ厌 2024-09-18 00:38:36

如果要考虑闰日,那么我建议使用此 lubridate 函数减去月份,因为其他方法将返回 3 月 1 日或 NA:

> library(lubridate)
> dt %m-% months(12*2)
[1] "2008-03-17"

# Try with leap day
> leapdt <- as.Date('2016/02/29')
> leapdt %m-% months(12*2)
[1] "2014-02-28"

If leap days are to be taken into account then I'd recommend using this lubridate function to subtract months, as other methods will return either March 1st or NA:

> library(lubridate)
> dt %m-% months(12*2)
[1] "2008-03-17"

# Try with leap day
> leapdt <- as.Date('2016/02/29')
> leapdt %m-% months(12*2)
[1] "2014-02-28"
初见你 2024-09-18 00:38:36

与 rcs 的答案相同,但可以在向量上操作它(回答 MichaelChirico,我无法评论我没有足够的代表):

R> unlist(lapply(c("2015-12-01", "2016-12-01"), 
      function(x) { return(as.character(seq(as.Date(x), length=2, by="-1 years")[2])) }))
 [1] "2014-12-01" "2015-12-01"

Same answer than the one by rcs but with the possibility to operate it on a vector (to answer to MichaelChirico, I can't comment I don't have enough rep):

R> unlist(lapply(c("2015-12-01", "2016-12-01"), 
      function(x) { return(as.character(seq(as.Date(x), length=2, by="-1 years")[2])) }))
 [1] "2014-12-01" "2015-12-01"
淡淡の花香 2024-09-18 00:38:36

这种方式似乎也能完成这项工作

dt = as.Date("2010/03/17")
dt-365*2
[1] "2008-03-17"

as.Date("2008/02/29")-365*2
## [1] "2006-03-01"

This way seems to do the job as well

dt = as.Date("2010/03/17")
dt-365*2
[1] "2008-03-17"

as.Date("2008/02/29")-365*2
## [1] "2006-03-01"
翻了热茶 2024-09-18 00:38:36
cur_date <- str_split(as.character(Sys.Date()), pattern = "-")
cur_yr <- cur_date[[1]][1]
cur_month <- cur_date[[1]][2]
cur_day <- cur_date[[1]][3]
new_year <- as.integer(year) - 2
new_date <- paste(new_year, cur_month, cur_day, sep="-")
cur_date <- str_split(as.character(Sys.Date()), pattern = "-")
cur_yr <- cur_date[[1]][1]
cur_month <- cur_date[[1]][2]
cur_day <- cur_date[[1]][3]
new_year <- as.integer(year) - 2
new_date <- paste(new_year, cur_month, cur_day, sep="-")
日记撕了你也走了 2024-09-18 00:38:36

使用Base R,您可以简单地使用以下内容,而无需安装任何软件包。

1) 将字符串转换为日期格式,在第二个参数中指定输入格式,以便 R 可以正确解释您的日期格式。

dt = as.Date('2010/03/17',"%Y/%m/%d")

注意:如果您现在查看环境选项卡,您将看到 dt 作为变量,其值为“2010-03-17”(年月日期用“-”而不是“/”分隔)

2)指定减去多少年

years_substract=2

3)使用paste()结合format()只保留月份和日期并且只需从原始日期中减去2年。 Format() 函数将仅使用格式第二个参数相应地保留日期的特定部分。

dt_substract_2years<-
as.Date(paste(as.numeric(format(dt,"%Y"))-years_substract,format(dt,"%m"),format(dt,"%d"),sep = "-"))

注意1:我们使用paste()函数来连接日期组件并将分隔符指定为“-”(sep =“-”),默认情况下日期的R分隔符。

注意2:我们还使用 as.numeric() 函数将年份从字符转换为数字

Using Base R, you can simply use the following without installing any package.

1) Transform your character string to Date format, specifying the input format in the second argument, so R can correctly interpret your date format.

dt = as.Date('2010/03/17',"%Y/%m/%d")

NOTE: If you look now at your enviroment tab you will see dt as variable with the following value "2010-03-17" (Year-month-date separated by "-" not by "/")

2) specify how many years to substract

years_substract=2

3) Use paste() combined with format () to only keep Month and Day and Just substract 2 year from your original date. Format() function will just keep the specific part of your date accordingly with format second argument.

dt_substract_2years<-
as.Date(paste(as.numeric(format(dt,"%Y"))-years_substract,format(dt,"%m"),format(dt,"%d"),sep = "-"))

NOTE1: We used paste() function to concatenate date components and specify separator as "-" (sep = "-")as is the R separator for dates by default.

NOTE2: We also used as.numeric() function to transform year from character to numeric

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文