[R] Formatting outputs:(chronological object)

jim holtman jholtman at gmail.com
Wed Oct 7 14:52:39 CEST 2009


try this  --  the example you provided did not work, so I had to hack
together an example:

library(chron);library(zoo)
date<- seq(as.Date("1990-01-01"), as.Date("2000-12-31"), by = 1)
obs<-rnorm(length(date), mean = 10, sd = 2.5)
monthly<- function(date) as.Date(as.yearmon(date))
obs <- tapply(obs, monthly(date), sum)
obs.yr <- split(obs, substring(names(obs), 1, 4))
for (i in names(obs.yr)){
    cat("Observations for Year", i, '\n')
    print(cbind(obs.yr[[i]]))
    cat("Total for Year", i, sum(obs.yr[[i]]), '\n\n')
}

Observations for Year 1990
               [,1]
1990-01-01 336.1179
1990-02-01 279.0572
1990-03-01 314.8671
1990-04-01 281.0801
1990-05-01 288.1853
1990-06-01 325.3545
1990-07-01 334.3680
1990-08-01 314.9724
1990-09-01 300.5801
1990-10-01 324.7640
1990-11-01 307.5808
1990-12-01 300.1142
Total for Year 1990 3707.042

Observations for Year 1991
               [,1]
1991-01-01 314.4718
1991-02-01 269.0690
1991-03-01 309.8057
1991-04-01 287.8462
1991-05-01 337.0155
1991-06-01 299.6412
1991-07-01 310.2771
1991-08-01 271.6742
1991-09-01 293.7720
1991-10-01 301.3979
1991-11-01 294.1190
1991-12-01 310.4470
Total for Year 1991 3599.537


On Wed, Oct 7, 2009 at 8:11 AM, Subodh Acharya <shoebodh at gmail.com> wrote:
> Hello everyone,
>
> I have a data generated in a way similar to the following
>
> library(chron);library(zoo)
> date<- seq(as.Date("1990-01-01"),, as.Date("2000-12-31"), by = 1)
> obs<- zoo(rnorm(length(date), mean = 10, sd = 2.5), order.by = date)
> monthly<- function(date) as.Date(as.yearmon(Date))
> result<- data.frame ( Date = obs = aggregate(obs, monthly, sum))
>
> Now,
> I want to get the result data in the following format instead of the
>  continuous table
> Observation for year 1990,
> 1990-01-01 314.7547
> 1990-02-01 266.0105
> 1990-03-01 353.7275
> 1990-04-01 273.8321
> 1990-05-01 325.3691
> 1990-06-01 253.1501
> 1990-07-01 318.1684
> 1990-08-01 291.2097
> 1990-09-01 298.6409
> 1990-10-01 304.0167
> 1990-11-01 279.4668
> 1990-12-01 286.9456
> Total for year 1990 = xxx
>
> Observation for year 1991
> 1991-01-01 313.1515
> 1991-02-01 297.9274
> 1991-03-01 315.2565
> 1991-04-01 290.9797
> 1991-05-01 327.0480
> 1991-06-01 281.5030
> 1991-07-01 316.7332
> 1991-08-01 289.9122
> 1991-09-01 296.9334
> 1991-10-01 280.2053
> 1991-11-01 295.6098
> 1991-12-01 321.6451
> Total for year 1991 = xxx
>
>
> Any help would be highly appreciated
>
> Thanks
>
>
>
>
>
> --
> Acharya, Subodh
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list