0

我有以下带有日期和计数的数据。请帮助转换这一行,其中月份是列。行是每年的数据

Date       count
=================  
2011-01-01 10578
2011-02-01  9330
2011-03-01 10686
2011-04-01 10260
2011-05-01 10032
2011-06-01  9762
2011-07-01 10308
2011-08-01  9966
2011-09-01 10146
2011-10-01 10218
2011-11-01  8826
2011-12-01  9504
to    
       JAN   FEB      MAR    APR   MAY  JUN   JUL  AUG  SEP    OCT NOV   DEC  
------------------------------------------------------------------------------
2011  10578   9330   10686 10260 10032 9762 10308 9966 10146 10218 8826 9504
2012 ....
4

2 回答 2

2

ts这是R 基础中的完美任务。假设您的 data.frame 正在x使用ts将产生您想要的输出。

> ts(x$count, start=c(2011,01,01), end=c(2011,12,01), frequency=12)
       Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
2011 10578  9330 10686 10260 10032  9762 10308  9966 10146 10218  8826  9504
于 2013-11-06T09:28:59.620 回答
1

如果您的数据在x尝试这样的事情:

library(reshape2)
res <- dcast(transform(x, month = format(Date, format="%m"),
                       year = format(Date, "%Y")),
             year ~ month, value.var="count")
rownames(res) <- res$year
res <- res[,-1]
names(res) <- toupper(month.abb[as.numeric(names(res))])
res

这假设这x$Date已经是一个日期。如果没有,您需要先将 is 转换为日期:

x$Date <- as.Date(x$Date)
于 2013-11-06T09:11:39.513 回答