Count days of current year

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Count days of current year

Felipe Carrillo
Hi:
Is there a function that counts the number of days of any given or current year based on the date?  

Felipe D. Carrillo  
Supervisory Fishery Biologist  
Department of the Interior  
US Fish & Wildlife Service  
California, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Count days of current year

Thomas Petzoldt-4
Felipe Carrillo wrote:
> Hi:
> Is there a function that counts the number of days of any given or current year based on the date?  

See ?Date and ?strptime and try the following:

y <- as.numeric(format(as.Date("2008-11-25"), "%Y"))

as.numeric(format(as.Date(paste(y, "12", "31", sep="-")), "%j"))


HTH Thomas P.

>
> Felipe D. Carrillo  
> Supervisory Fishery Biologist  
> Department of the Interior  
> US Fish & Wildlife Service  
> California, USA
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Count days of current year

Gabor Grothendieck
In reply to this post by Felipe Carrillo
As yearmon represents year/month as year + fraction of year
adding 1 gives next year.  The first output below
gives an answer of class "difftime" whereas the the second
solution is "numeric":

> library(zoo)
> year <- 2000:2010
> d <- as.Date(as.yearmon(year)+1) - as.Date(as.yearmon(year))
> d
Time differences in days
 [1] 366 365 365 365 366 365 365 365 366 365 365

> # or to get a numeric answer:
> as.double(d, units = "days")
 [1] 366 365 365 365 366 365 365 365 366 365 365



On Mon, Nov 24, 2008 at 2:25 PM, Felipe Carrillo
<[hidden email]> wrote:

> Hi:
> Is there a function that counts the number of days of any given or current year based on the date?
>
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Count days of current year

hadley wickham
In reply to this post by Felipe Carrillo
Why not write it yourself?

days_in_year <- function(year) {
  365 + (year %% 4 == 0) - (year %% 100 == 0) + (year %% 400 == 0)
}

This should work for any year in the Gregorian calendar.

Hadley

On Mon, Nov 24, 2008 at 1:25 PM, Felipe Carrillo
<[hidden email]> wrote:

> Hi:
> Is there a function that counts the number of days of any given or current year based on the date?
>
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
>
> ______________________________________________
> [hidden email] 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.
>



--
http://had.co.nz/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Count days of current year

Gabor Grothendieck
Similarly tis and chron have nearly the identical function:

> library(tis)
> 365 + isLeapYear(2000:2010)
 [1] 366 365 365 365 366 365 365 365 366 365 365

> isLeapYear
function (y)
y%%4 == 0 & (y%%100 != 0 | y%%400 == 0)
<environment: namespace:tis>

> library(chron)
> 365 + leap.year(2000:2010)
 [1] 366 365 365 365 366 365 365 365 366 365 365
> leap.year
function (y)
{
    if (inherits(y, "dates"))
        y <- month.day.year(as.numeric(y), origin. = origin(y))$year
    y%%4 == 0 & (y%%100 != 0 | y%%400 == 0)
}
<environment: namespace:chron>


On Tue, Nov 25, 2008 at 9:38 AM, hadley wickham <[hidden email]> wrote:

> Why not write it yourself?
>
> days_in_year <- function(year) {
>  365 + (year %% 4 == 0) - (year %% 100 == 0) + (year %% 400 == 0)
> }
>
> This should work for any year in the Gregorian calendar.
>
> Hadley
>
> On Mon, Nov 24, 2008 at 1:25 PM, Felipe Carrillo
> <[hidden email]> wrote:
>> Hi:
>> Is there a function that counts the number of days of any given or current year based on the date?
>>
>> Felipe D. Carrillo
>> Supervisory Fishery Biologist
>> Department of the Interior
>> US Fish & Wildlife Service
>> California, USA
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>
>
> --
> http://had.co.nz/
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.