# [R] Sort data by month

arun smartpink111 at yahoo.com
Thu May 23 07:52:30 CEST 2013

```HI Atem,
No problem.
If you want to do some calculations based on monthly data:
aggregate(z1,month(index(z1)),mean)
#      1        2        3        4        5        6        7        8
#21.52044 12.64133 20.06044 27.28711 43.41489 70.15022 41.18756 34.28689
#      9       10       11       12
#37.13556 19.30778 18.39089 19.32711

#Using lst1

sapply(lst1,mean)
#       1        2        3        4        5        6        7        8
#21.52044 12.64133 20.06044 27.28711 43.41489 70.15022 41.18756 34.28689
#      9       10       11       12
#37.13556 19.30778 18.39089 19.32711

Regards,
Arun

________________________________
From: "zilefacelvis at yahoo.com" <zilefacelvis at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Thursday, May 23, 2013 1:42 AM
Subject: Re: Re: [R] Sort data by month

Hi AK,

Thanks so much. Problem well handled.

Atem.

------ Original Message ------

From : arun
>To : Zilefac Elvis;
>Cc : R help;
>Sent : 22-05-2013 23:36
>Subject : Re: [R] Sort data by month
>
>Hi, dat1<- read.csv("me.csv",header=TRUE,stringsAsFactors=FALSE) dat1\$y<- as.Date(dat1\$y,format="%d/%m/%Y")
library(zoo)
z1<- zoo(dat1[,-1],dat1[,1])
library(xts)
library(lubridate)
lst1<-lapply(split(z1,month(index(z1))),as.xts)
head(lst1[[1]])
#            [,1]
#1961-01-01  7.45
#1962-01-01 17.63
#1963-01-01 52.23
#1964-01-01 26.92
#1965-01-01 22.11
#1966-01-01 67.43 tail(lst1[[12]])
#            [,1]
#2000-12-01 17.90
#2001-12-01  8.69
#2002-12-01  8.21
#2003-12-01  5.94
#2004-12-01 39.94
#2005-12-01 10.35 #or
you can convert to data.frame
datNew<-do.call(rbind,lapply(lst1,as.data.frame))
row.names(datNew)<-gsub("^.*\\.","",row.names(datNew)) head(datNew)
#              V1
#1961-01-01  7.45
#1962-01-01 17.63
#1963-01-01 52.23
#1964-01-01 26.92
#1965-01-01 22.11
#1966-01-01 67.43 head(dat1)
#           y     x
#1 1961-01-01  7.45
#2 1961-02-01  7.92
#3 1961-03-01  7.34
#4 1961-04-01 15.08
#5 1961-05-01 25.66
#6 1961-06-01 26.78 #or
x1<- as.xts(z1)
datNew2<-data.frame(x=coredata(x1)[order(month(index(x1)))])
rownames(datNew2)<-index(x1)[order(month(index(x1)))] colnames(datNew)<-"x" identical(datNew,datNew2)
#[1] TRUE
A.K. ________________________________
From: Zilefac Elvis
To: arun
Cc: R help
Sent: Thursday, May 23, 2013 12:32 AM
Subject: Re: [R] Sort data by month Hi Ak,
I seems to work correctly with your data. Please try it on my data (attached). My data is monthly.
The y in column 1 is not provided when I convert my data to zoo.
So, delete the header, y and try to work with the data.
Sort all Jans, Febs, etc just as you did.
Atem. ________________________________
From: arun
To: Zilefac Elvis
Cc: R help
Sent: Wednesday, May 22, 2013 8:47 PM
Subject: Re: [R] Sort data by month Hi,
May be this helps. date1<-seq.Date(as.Date("01/01/2000",format="%d/%m/%Y"),as.Date("31/12/2010",format="%d/%m/%Y"),by="day")
set.seed(24) value<- rnorm(4018,25) dat1<- data.frame(date1,value) dat2<- do.call(rbind,split(dat1,as.numeric(gsub(".*\\-(.*)\\-.*","\\1",dat1\$date1)))) library(zoo)
z1<- as.zoo(dat2) tail(z1,50)     date1      value
3969 2009-12-13 24.82354
3970 2009-12-14 25.01100
3971 2009-12-15 24.22370
3972 2009-12-16 26.20387
3973 2009-12-17 23.94170
3974 2009-12-18 24.39037
3975 2009-12-19 25.01231
3976 2009-12-20 25.25939
3977 2009-12-21 23.57236
3978 2009-12-22 26.42321
3979 2009-12-23 24.72704
3980 2009-12-24 24.60001
3981 2009-12-25 24.23091
3982 2009-12-26 23.63178
3983 2009-12-27 25.02472
3984 2009-12-28 25.58247
3985 2009-12-29 24.79180
3986
2009-12-30 24.93262
3987 2009-12-31 23.96313
3988 2010-12-01 25.83565
3989 2010-12-02 24.45985
3990 2010-12-03 25.17499
3991 2010-12-04 24.57330
3992 2010-12-05 24.52134
3993 2010-12-06 24.71778
3994 2010-12-07 25.81350
3995 2010-12-08 25.35647
3996 2010-12-09 24.50438
3997 2010-12-10 24.12099
3998 2010-12-11 25.16103
3999 2010-12-12 24.03809
4000 2010-12-13 24.31008
4001 2010-12-14 24.09804
4002 2010-12-15 25.58613
4003 2010-12-16 25.19094
4004 2010-12-17 25.75322
4005 2010-12-18 24.30318
4006 2010-12-19 24.79664
4007 2010-12-20 23.58302
4008 2010-12-21 24.37014
4009 2010-12-22 23.28147
4010 2010-12-23 23.70687
4011 2010-12-24 26.17041
4012 2010-12-25 24.76579
4013 2010-12-26 25.49849
4014 2010-12-27 23.64199
4015 2010-12-28 23.97338
4016 2010-12-29 24.96617
4017 2010-12-30 24.07872
4018 2010-12-31 25.25245
A.K. ----- Original Message
-----
From: Zilefac Elvis
To: "r-help at r-project.org"
Cc:
Sent: Wednesday, May 22, 2013 11:11 PM
Subject: [R] Sort data by month Hello,
I have a 'zoo' object containing dates [dd/mm/yr] in the first column and values in the second column.
I tried as.Date but did not succeed.
Here is an example of date format 01/01/2000 01/02/2000
...
01/12/2000
01/01/2001
01/02/2001 ...
01/12/2000  etc.
I would like to sort all Jans from 2000 to 2010, all Febs from 2000 to 2010 ... all Decs from 2000 to 2010.
So, basically I would like to sort by month. Thanks for your
help.
Atem.    [[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.

```

More information about the R-help mailing list