[R] leap year and order function

Toby Marthews toby.marthews at ouce.ox.ac.uk
Mon Jan 31 14:49:41 CET 2011


Dear All,

I've always used this code:

year=c(1948:1953,2000,2100,2200,2300)
numdays=ifelse((year%%4==0 & year%%100!=0) | year%%400==0,366,365)

> numdays
 [1] 366 365 365 365 366 365 366 365 365 365

Toby

________________________________________
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Dennis Murphy [djmuser at gmail.com]
Sent: 31 January 2011 11:51
To: Bill.Venables at csiro.au
Cc: R-help at r-project.org
Subject: Re: [R] leap year and order function

Hi:

There is an additional proviso:

If the year is divisible by 100, then the next test is whether it's
divisible by 400. If both conditions are met, then the century year is a
leap year. Hence, 2000 was a leap year because it is divisible by 400, but
2100, 2200 and 2300 will not be. For Perl and Ruby code, see

http://feb29.leapyearday.com/freecode.html

The 'correct' code is in a link associated with the Note below the first
paragraph.

HTH,
Dennis

On Sun, Jan 30, 2011 at 9:16 PM, <Bill.Venables at csiro.au> wrote:

> > yearLength <- function(year) 365 + (year %% 4 == 0)
>
>
> > yearLength(1948:2010)
>  [1] 366 365 365 365 366 365 365 365 366 365 365 365 366 365 365 365 366
> 365 365 365 366
> [22] 365 365 365 366 365 365 365 366 365 365 365 366 365 365 365 366 365
> 365 365 366 365
> [43] 365 365 366 365 365 365 366 365 365 365 366 365 365 365 366 365 365
> 365 366 365 365
> >
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Bobby Lee
> Sent: Monday, 31 January 2011 2:23 PM
> To: R-help at r-project.org
> Subject: [R] leap year and order function
>
> im trying to write a for loop so that every leap year, the number of days
> becomes to 366 instead of 365. could someone help me out?
> and also, this set of data has 99.99s I set all the 99.99 ==NA.
> however, when im doing the order function to find the max value of that
> year, it still reads 99.99 as the max value.
> Thank you very much
>
> maxday <- matrix(NA,63,5)
>
> for (a in 1948:2010){
>
> maxday[,1]<-1948:2010
> yearly<-na.omit(dat.mat[dat.mat[,1]==a,])
>
> maxday[a-1947,2]<-yearly[order(yearly[,4])[*365*],2]
> maxday[a-1947,3]<-yearly[order(yearly[,4])[*365*],3]
>
>
> maxday[63,2]<-yearly[order(yearly[,4])[127],2]
> maxday[63,3]<-yearly[order(yearly[,4])[127],3]
> maxday[a-1947,4]<-max(yearly[,4])
> maxday[,5]<-len[,2]
> }
>
>        [[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