2012-08-09 99 views
2

我有这样的数据集,并试图转换为日期,这样我可以列为图表:转换因子迄今为止R中

dput(y) 
structure(c(23L, 17L, 41L, 1L, 47L, 35L, 29L, 7L, 63L, 58L, 53L, 
12L, 24L, 18L, 42L, 2L, 48L, 36L, 30L, 8L, 64L, 59L, 54L, 13L, 
25L, 19L, 43L, 3L, 49L, 37L, 31L, 9L, 65L, 60L, 55L, 14L, 26L, 
20L, 44L, 4L, 50L, 38L, 32L, 10L, 66L, 61L, 56L, 15L, 27L, 21L, 
45L, 5L, 51L, 39L, 33L, 11L, 67L, 62L, 57L, 16L, 28L, 22L, 46L, 
6L, 52L, 40L, 34L), .Label = c("APR-07", "APR-08", "APR-09", 
"APR-10", "APR-11", "APR-12", "AUG-07", "AUG-08", "AUG-09", "AUG-10", 
"AUG-11", "DEC-07", "DEC-08", "DEC-09", "DEC-10", "DEC-11", "FEB-07", 
"FEB-08", "FEB-09", "FEB-10", "FEB-11", "FEB-12", "JAN-07", "JAN-08", 
"JAN-09", "JAN-10", "JAN-11", "JAN-12", "JUL-07", "JUL-08", "JUL-09", 
"JUL-10", "JUL-11", "JUL-12", "JUN-07", "JUN-08", "JUN-09", "JUN-10", 
"JUN-11", "JUN-12", "MAR-07", "MAR-08", "MAR-09", "MAR-10", "MAR-11", 
"MAR-12", "MAY-07", "MAY-08", "MAY-09", "MAY-10", "May-11", "MAY-12", 
"NOV-07", "NOV-08", "NOV-09", "NOV-10", "NOV-11", "OCT-07", "OCT-08", 
"OCT-09", "OCT-10", "OCT-11", "SEP-07", "SEP-08", "SEP-09", "SEP-10", 
"SEP-11"), class = "factor") 

我试图

y1 <- strptime(y, format = "%b-%y") 

Y1让所有NA,任何想法?

回答

6

你需要一天的时间,将其转换为一个日期:

##Make each date the first of the month 
strptime(paste("1", as.character(y)), format="%d %b-%y") 
+0

非常感谢你 – user1471980 2012-08-09 20:29:57

1

如果你将时间序列上月规模regualr基础上开展工作,你可能也想看看动物园包,其中作为不需要添加“每月第一天价值”的“年度日历”课程。它有许多相关的绘图和计算方法。广泛使用的'xts'包基于'动物园'结构。

require(zoo) 
zym <- as.yearmon(as.character(y), "%b-%y") 
str(zym) 
#Class 'yearmon' num [1:67] 2007 2007 2007 2007 2007 ... 
head(zym) 
#[1] "Jan 2007" "Feb 2007" "Mar 2007" "Apr 2007" "May 2007" "Jun 2007" 
format(head(zym), "%b %y") 
#[1] "Jan 07" "Feb 07" "Mar 07" "Apr 07" "May 07" "Jun 07" 
+0

非常感谢你 – user1471980 2012-08-10 13:14:34