如何在R中减去年份? [英] How to subtract years in R?

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

问题描述

我在R中有日期,例如

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

我想从这个日期减去2年,而不用担心闰年和这些问题, 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'). How would I do that? Thanks!

推荐答案

最简单的做法是将其转换为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中的天数

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

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