在使用strptime将日期/时间字符串转换为POSIXlt后,我将看到以下内容(这里的数据被截断):
DateTime North South West East Seast System 1 2008-09-12 01:00:00 1919.9 3721.4 2085.9 2565.5 2571.1 12863.8
2008-09-12 02:00:00 1827.0 3518.1 1965.3 2396.9 2410.7 12118.0
2008-09-12 03:00:00 1755.4 3388.4 1866.8 2338.7 2335.2 11684.5
4 2008-09-12 04:00:00 1733.5 3327.1 1810.0 2295.6 2290.2 11456.4
5 2008-09-12 05:00:00 1742.7 3327.3 1831.4 2314.2 2302.3 11517.9
6 2008-09-12 06:00:00 1912.2 3504.4 1986.7 2515.0 2502.6 12420.9
然后,我使用以下代码片段将数据(似乎是正确的)聚合为年月平均值:North_Monthly_Avg <- aggregate(North, list(Date=format(DateTime, "%Y-%m")),mean),它生成以下内容:
日期x
1 2008-09 2192.066
2008年2-10 1885.074
2008年3-11 1675.373
4 2008-12 1637.231
5 2009-01 1752.693
6 2009-02 1743.393
我可以画出'x‘值,但不能得到在x轴上正确标注的年份月份,因为它只是在绘制索引。不知道我错过了什么.我和axis.POSIXct一起玩过,但没有运气。
发布于 2011-11-23 18:10:21
试试zoo和lattice
library(zoo)
library(lattice)
dat <- 'Date Time North South West East Seast System
2008-09-12 01:00:00 1919.9 3721.4 2085.9 2565.5 2571.1 12863.8
2008-09-12 02:00:00 1827.0 3518.1 1965.3 2396.9 2410.7 12118.0
2008-09-12 03:00:00 1755.4 3388.4 1866.8 2338.7 2335.2 11684.5
2008-09-12 04:00:00 1733.5 3327.1 1810.0 2295.6 2290.2 11456.4
2008-09-12 05:00:00 1742.7 3327.3 1831.4 2314.2 2302.3 11517.9
2008-09-12 06:00:00 1912.2 3504.4 1986.7 2515.0 2502.6 12420.9'
z <- read.zoo(text = dat, header = TRUE, index.column = 1:2, tz = "")
xyplot(z)
zAgg <- aggregate(z$North, by = as.yearmon, FUN = mean)
dat2 <- 'Date x
2008-09 2192.066
2008-10 1885.074
2008-11 1675.373
2008-12 1637.231
2009-01 1752.693
2009-02 1743.393'
zAgg <- read.zoo(text = dat2, header = TRUE, FUN = as.yearmon)
plot(zAgg, xaxt = "n")
tt <- time(zAgg)
m <- format(tt, "%m")
axis(side = 1, at = tt, labels = ifelse(m == "01", trunc(tt), m), cex.axis = .7)发布于 2011-11-23 18:05:46
尝试在日期上使用as.integer()
North_Monthly_Avg <- aggregate(North, list(Date=as.integer(format(DateTime, "%Y-%m"))),mean)发布于 2011-11-25 09:09:01
@ your 1062431,要将勾名编辑成您喜欢的格式,请编辑奥斯卡答案中的m <- format(tt,"%m")行。
要获得12-2008格式,您需要修改:
M <-格式(tt,"%m")到m <-格式(tt,"%m - %Y")
要获得2008年12月的格式,您需要修改:
M <-格式(tt,"%m")到m <-格式(tt,"%b %Y")
https://stackoverflow.com/questions/8246645
复制相似问题