[R] last day of month values

M?der J?rg maeder at atmos.umnw.ethz.ch
Tue Mar 19 13:16:37 CET 2002


hello lukas,

new try

#dataframe with date and value column. Date as to be a POSIXct (see
as.POSIXct,asPOSIXlt,strftime,strptime)
dm <-
data.frame(date=as.POSIXct(strptime(c('12-02-02','16-02-02','13-03-02','18-03-02','01-02-01','04-03-02'),format='%d-%m-%y')),value=c(2,5,3,6,8,6))

#small function that we need. Find the x with the last day
dy <- function(x){
  y <-  as.numeric(strftime(as.POSIXlt(x),format='%d'))
  y <- match(max(y),y)
  x[y]
}

#use this function for each month-year combination 
#and return the day as dirty numeric 
ma <- tapply(dm$date,strftime(as.POSIXlt(dm$date),'%Y %m'),dy)

#find the rigth row-index for ma
i <- match(ma,as.numeric(dm$date))

#use this to access your data
dm$date[i]
dm$value[i]
dm[i,]

special feature: the index is sorted in time

is it this????

gruess

joerg
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Thank you, it works. But how do I deal with years? This finds just the
> "maxdayspermonths" within a year. But my data are for more years. And
> the maxday in month differs in years.
> Thank you.
> 
> lukas
> 
> On Tue, 26 Feb 2002, Joerg Maeder wrote:
> 
>> Hi Lukas
>>
>> use the functions stpftime and strptime (see R documentation)
>> eg: as.numeric(strftime(f,format="%m")) for the months, year: "%y",
>> day: "%d"
>> Be carefull about leadings 0 (3 or 03) and try to write the year in 4
>> digits!
>> if your date is a string you can use as.numeric(substring(date,from,
>> length))
>> eg: month <- as.numeric(substring(date,4,2))
>>
>>
>> Lukas Kubin wrote:
>> >
>> > -----BEGIN PGP SIGNED MESSAGE-----
>> > Hash: SHA1
>> >
>> > Hi Joerg,
>> > thank you for the answer, however I have the timestamp in format
>> > 26-02-02. Do I have to extract each part of the date?
>> > Thank you.
>> >
>> > lukas
>> >
>> > On Tue, 26 Feb 2002, Joerg Maeder wrote:
>> >
>> > > Hallo Lukas,
>> > >
>> > > try this code
>> > > #data
>> > > d <-
>> > >
data.frame(day=c(1,10,15,30,2,16,18,28),month=c(1,1,1,1,2,2,2,2),value=c(8,9,7,5,6,4,1,2))
>> > > #find the highest day per month
>> > > ma <- tapply(d$day,d$month,max)
>> > > #get them values
>> > > d$value[match(as.numeric(names(ma))+(ma-1)/31,d$month+(d$day-1)/31)]
>> > >
>> > > gruess
>> > >
>> > > joerg
>> > >
>> > > Lukas Kubin wrote:
>> > > >
>> > > > -----BEGIN PGP SIGNED MESSAGE-----
>> > > > Hash: SHA1
>> > > >
>> > > > I have a stock market trading values time series. What's the
>> > > > best way to extract the "last day of month" values. I looked at
>> > > > function window() but doesn't appear suitable for this since it
>> > > > expects regular dates. Thank you.
>> > > >
>> > > > lukas
>> > > >
>> > > > - --
>> > > > Lukas Kubin
>> > > > lukas.kubin at permonik.com
>> > > > phone: 00420603836180
>> > > > -----BEGIN PGP SIGNATURE-----
>> > > > Version: GnuPG v1.0.5 (GNU/Linux)
>> > > > Comment: For info see http://www.gnupg.org
>> > > >
>> > > > iD8DBQE8e2w74TIZ2lmUAtsRAj/SAJ4heMKveDwpCLPwnnJTBnAvCSIRrQCeJTHi
>> > > > YEujkKsmoX/orLUlnHxBNYA=
>> > > > =H9uw
>> > > > -----END PGP SIGNATURE-----
>> > > >
>> > > >
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
>> > > > r-help mailing list -- Read
>> > > > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info",
>> > > > "help", or "[un]subscribe"
>> > > > (in the "body", not the subject !)  To:
>> > > > r-help-request at stat.math.ethz.ch
>> > > >
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>> > >
>> > >
>> >
>> > - --
>> > Lukas Kubin
>> > lukas.kubin at permonik.com
>> > phone: 00420603836180
>> > -----BEGIN PGP SIGNATURE-----
>> > Version: GnuPG v1.0.5 (GNU/Linux)
>> > Comment: For info see http://www.gnupg.org
>> >
>> > iD8DBQE8e5uJ4TIZ2lmUAtsRAuQXAKDCaNnEyXXVFa6fMS81klJIXDFrlgCgmm71
>> > zVnHeNICrNs6dvQd0Ir1YkI=
>> > =pcXi
>> > -----END PGP SIGNATURE-----
>>
>>
> 
> - -- 
> Lukas Kubin
> lukas.kubin at permonik.com
> phone: 00420603836180
> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.0.5 (GNU/Linux)
> Comment: For info see http://www.gnupg.org
> 
> iD8DBQE8kiht4TIZ2lmUAtsRAlXYAJ9AXzlfmDeh6A1qQFuZPUyVr/nrOgCfaGMd
> lgdIrgosXKzRjMNtKcqt/rA=
> =ee3Z
> -----END PGP SIGNATURE-----
> 
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read
> http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or
> "[un]subscribe"
> (in the "body", not the subject !)  To:
> r-help-request at stat.math.ethz.ch
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list