如何在R中减去日子? [英] How to subtract days in R?
问题描述
我正在尝试构建文件夹来存储数据。我想在文件夹的日期标注文件夹。
I'm trying to build folders to store data pulls. I want to label the folders with the day of that data in the pull.
Ex。我拉5天前从mysql的数据我想从5天前命名该文件夹日期。
Ex. I pull 5 days ago data from mysql i want to name the folder the date from 5 days ago.
MySQL可以轻松处理日期算术。我不知道R是如何做的。我应该在POSIXct中减去适当的秒数,然后转换为POSIXlt命名文件夹MM_DD_YYYY?
MySQL can easily handle date arithmetic. I'm not sure exactly how R does it. Should i just subtract the appropriate number of seconds in POSIXct and then convert to POSIXlt to name the folder MM_DD_YYYY?
还是有更好的方法?
推荐答案
只需减去一个数字:
> as.Date("2009-10-01")
[1] "2009-10-01"
> as.Date("2009-10-01")-5
[1] "2009-09-26"
由于 Date
类只有几天,您只需对其进行基本算术。
Since the Date
class only has days, you can just do basic arithmetic on it.
如果你想使用POSIXlt,因为某些原因,你可以使用它的插槽:
If you want to use POSIXlt for some reason, then you can use it's slots:
> a <- as.POSIXlt("2009-10-04")
> names(unclass(as.POSIXlt("2009-10-04")))
[1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst"
> a$mday <- a$mday - 6
> a
[1] "2009-09-28 EDT"
这篇关于如何在R中减去日子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!