如何减去年份? [英] How to subtract years?

查看:144
本文介绍了如何减去年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我想从这个日期开始减去2年,而不必担心leap年和此类问题,而得到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('2010-03-17').

我该怎么做?

推荐答案

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

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"

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

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

这篇关于如何减去年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆