在R,它可能格式化POSIXlt日期时间目标为一个月:
format(Sys.time(), format='%Y-%m')
有没有办法做的季节,或3个月组(DJF,MAM,JJA,SON)是一回事吗? 这些部门是在气候和生态科学非常常见的,这将是巨大的,有一个整洁的方式将它们与几个月飞快地像格式化。 显然DJF下降超过2年,但目的还是这个问题,是没有真正的问题 - 只是一直推插入任一年度,(或理想,这将是很好的能够指定他们进入哪一年) 。
我使用的输出作为指数by()
所以输出格式并没有多大关系,只要你将每年/季节是独一无二的。
编辑:示例数据:
dates <- Sys.Date()+seq(1,380, by=35)
dates <- structure(c(16277, 16312, 16347, 16382, 16417, 16452, 16487,
16522, 16557, 16592, 16627), class = "Date")
dates
#[1] "2014-07-26" "2014-08-30" "2014-10-04" "2014-11-08" "2014-12-13"
# "2015-01-17" "2015-02-21" "2015-03-28" "2015-05-02" "2015-06-06" "2015-07-11"
应导致:
c("2014-JJA", "2014-JJA", "2014-SON", "2014-SON", "2015-DJF", "2015-DJF",
"2015-DJF", "2015-MAM", "2015-MAM", "2015-JJA", "2015-JJA")
但是,“2015年 - DJF” S也可能是“2014 - DJF”。 此外,输出的形式并不重要 - “2104q4或201404也将被罚款。
as.POSIXlt
返回命名列表(这使得它不适合data.frame列)。 该列表的列可以单独访问,包括“年”(1900为基础,不同于用于默认1970)和“星期一”(从0开始)。 看到这份名单中HTE帮助系统最好的地方就是?DateTimeClasses
起初只是四季计算,那么一年,四季计算
c('DJF', 'MAM', 'JJA', 'SON')[ # select from character vector with numeric vector
1+((as.POSIXlt(dates)$mon+1) %/% 3)%%4]
[1] "JJA" "JJA" "SON" "SON" "DJF" "DJF" "DJF" "MAM" "MAM" "JJA"
[11] "JJA"
paste( 1900 + # this is the base year for POSIXlt year numbering
as.POSIXlt( dates )$year +
1*(as.POSIXlt( dates )$year==12) , # offset needed for December
c('DJF', 'MAM', 'JJA', 'SON')[ # indexing from 0-based-mon
1+((as.POSIXlt(dates)$mon+1) %/% 3)%%4]
, sep="-")
[1] "2014-JJA" "2014-JJA" "2014-SON" "2014-SON" "2014-DJF"
[6] "2015-DJF" "2015-DJF" "2015-MAM" "2015-MAM" "2015-JJA"
[11] "2015-JJA"
不应该是很难作出这样的构造您所期望的格式的功能。 这仅仅是在模的年份和月份的POSIXlt值算术。
我喜欢使用查找矢量这些各种各样的问题,例如:
x <- as.POSIXlt(
seq.Date(as.Date("2000-01-01"),as.Date("2002-01-01"),by="2 months")
)
例如,如果要指定南半球的季节,你可以这样做:
src <- rep(c("su","au","wi","sp"),each=3)[c(2:12,1)]
paste(format(x,"%Y-%m"),src[x$mon+1])
# [1] "2000-01 su" "2000-03 au" "2000-05 au" "2000-07 wi" "2000-09 sp"
# [6] "2000-11 sp" "2001-01 su" "2001-03 au" "2001-05 au" "2001-07 wi"
#[11] "2001-09 sp" "2001-11 sp" "2002-01 su"
更改src
你认为合适的重新标记的类别名称。
让Q1是DJF; Q2,MAM; 等等,那么:
seasonal.quarters <- function(x) {
x <- as.POSIXlt(x)
x$mon <- (x$mon + 1) %% 12
quarters(x)
}
options(stringsAsFactors=FALSE)
nonleap.year <- seq(from=as.POSIXct('2013-1-1'), to=as.POSIXct('2014-1-1'), by='day')
d <- data.frame(ms=months(nonleap.year), qs=seasonal.quarters(nonleap.year))
by(d, INDICES=list(d$qs), FUN=function(x) unique(x$ms))
# : Q1
# [1] "January" "February" "December"
# -------------------------------------
# : Q2
# [1] "March" "April" "May"
# -------------------------------------
# : Q3
# [1] "June" "July" "August"
# -------------------------------------
# : Q4
# [1] "September" "October" "November"
leap.year <- seq(from=as.POSIXct('2016-1-1'), to=as.POSIXct('2017-1-1'), by='day')
d <- data.frame(ms=months(leap.year), qs=seasonal.quarters(leap.year))
by(d, INDICES=list(d$qs), FUN=function(x) unique(x$ms))
# : Q1
# [1] "January" "February" "December"
# -------------------------------------
# : Q2
# [1] "March" "April" "May"
# -------------------------------------
# : Q3
# [1] "June" "July" "August"
# -------------------------------------
# : Q4
# [1] "September" "October" "November"
这是另一种42的回答以上。 去看看我的评论在那里我张贴它的原因。
dates_orig <- as.POSIXlt(c("2013-01-01", "2013-02-01", "2013-03-01", "2013-04-01", "2013-05-01", "2013-06-01", "2013-07-01", "2013-08-01", "2013-09-01", "2013-10-01", "2013-11-01", "2013-12-01", "2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01", "2014-07-01", "2014-08-01", "2014-09-01", "2014-10-01", "2014-11-01", "2014-12-01"))
format(dates_orig, format='%Y%b')
[1] "2013Jan" "2013Feb" "2013Mar" "2013Apr" "2013May" "2013Jun" "2013Jul" "2013Aug" "2013Sep" "2013Oct" "2013Nov" "2013Dec" "2014Jan" "2014Feb" "2014Mar"
[16] "2014Apr" "2014May" "2014Jun" "2014Jul" "2014Aug" "2014Sep" "2014Oct" "2014Nov" "2014Dec"
dates <- as.POSIXlt(dates_orig)
# shift Jan and Feb to the previous year
dates$year[dates$mon < 2] <- dates$year[dates$mon < 2] - 1
# convert months to seasons (named by first month of season)
dates$mon <- (((dates$mon - 2) %/% 3) %% 4) * 3 + 2
format(dates, format='%Y%b')
[1] "2012Dec" "2012Dec" "2013Mar" "2013Mar" "2013Mar" "2013Jun" "2013Jun" "2013Jun" "2013Sep" "2013Sep" "2013Sep" "2013Dec" "2013Dec" "2013Dec" "2014Mar"
[16] "2014Mar" "2014Mar" "2014Jun" "2014Jun" "2014Jun" "2014Sep" "2014Sep" "2014Sep" "2014Dec"