R将序列拆分为月份

R将序列拆分为月份,r,time-series,lubridate,R,Time Series,Lubridate,我有以下日期和计数的数据。请帮助转换这一行,其中月份为列。行是每年的数据 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-1

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

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 ....

如果数据位于
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
中,请尝试以下操作:

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
中,请尝试以下操作:

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
中,请尝试以下操作:

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

对于R base中的
ts
,这是一个完美的任务。假设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

对于R base中的
ts
,这是一个完美的任务。假设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

对于R base中的
ts
,这是一个完美的任务。假设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

对于R base中的
ts
,这是一个完美的任务。假设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

数据被正确分割。但几个月都不合适4月8月12月2月1月7月6月5月11月10月9日201110260 9966 9504 9330 10578 10308 9762 10686 10032 8826 10218 10146我已经修好了,但我更喜欢
ts
答案!数据被正确分割。但几个月都不合适4月8月12月2月1月7月6月5月11月10月9日201110260 9966 9504 9330 10578 10308 9762 10686 10032 8826 10218 10146我已经修好了,但我更喜欢
ts
答案!数据被正确分割。但几个月都不合适4月8月12月2月1月7月6月5月11月10月9日201110260 9966 9504 9330 10578 10308 9762 10686 10032 8826 10218 10146我已经修好了,但我更喜欢
ts
答案!数据被正确分割。但几个月都不合适4月8月12月2月1月7月6月5月11月10月9日201110260 9966 9504 9330 10578 10308 9762 10686 10032 8826 10218 10146我已经修好了,但我更喜欢
ts
答案!