1 生成年-月格式的日期序列
####生成1948年1月-2011年11月的日期序列,要求是年-月格式,不要日
###先生成年月日格式的数据,然后再调整到指定的格式
temp <- seq.Date(from = as.Date("1948/01/01",format = "%Y/%m/%d"), by = "month", length.out = 767)
head(temp)
输出:[1] "1948-01-01" "1948-02-01" "1948-03-01" "1948-04-01" "1948-05-01" "1948-06-01"
tail(temp)
输出:[1] "2011-06-01" "2011-07-01" "2011-08-01" "2011-09-01" "2011-10-01" "2011-11-01"
temp <- format(temp, format = "%Y/%m")
head(temp)
输出:[1] "1948/01" "1948/02" "1948/03" "1948/04" "1948/05" "1948/06"
来源:http://blog.sciencenet.cn/blog-247792-837263.html
2 生成日时间序列
### 指定起始时间
startdate <- as.Date("1960-01-01") #生成日序列
enddate <- as.Date("2011-12-31")
ndays <- enddate - startdate + 1
tt <- ts(1:ndays, frequency =1, start =as.Date("1960-01-01"))
ss <- as.Date("1960-01-01")
dates <- seq(from=ss, by=1, length.out=ndays)
tt <- data.frame(dates,tt,aa)
head(tt)
tail(tt)
来源:https://www.douban.com/note/607003723/
3 R语言生成月份递增序列
比如起始日期为:2015-11-15
该日期每月变更一次,下个月为 2015-12-15,接下来依此为:2016-01-15、2016-02-15...
install.packages("lubridate")
library(lubridate)
as.Date("2015-11-15")+months(1:20)
来源:
http://bbs.pinggu.org/forum.php?mod=viewthread&tid=4124079&page=1