[R] Changing date format

Frederic Ntirenganya ntfredo at gmail.com
Wed Oct 8 15:49:02 CEST 2014


The idea is that I want the non-leap years to be 366 days instead of being
365 days. ie. Each year must have 366 days.

for example: in the column Start2, Apr 18 will be Apr 17.

> head(Samaru)
  Year Start End Length Start2   End2
1 1930   108 288    180 Apr 18 Oct 15
2 1931   118 288    170 Apr 28 Oct 15
3 1932   115 295    180 Apr 24 Oct 21
4 1933   156 294    138 Jun 05 Oct 21
5 1934   116 291    175 Apr 26 Oct 18
6 1935   134 288    154 May 14 Oct 15

Is there a way to that in R?

On Tue, Oct 7, 2014 at 3:25 PM, Frederic Ntirenganya <ntfredo at gmail.com>
wrote:

> Thanks All. Your idea is useful!!!
>
> On Tue, Oct 7, 2014 at 1:01 PM, Jim Lemon <jim at bitwrit.com.au> wrote:
>
>> On Tue, 7 Oct 2014 11:51:34 AM Göran Broström wrote:
>> > On 2014-10-07 11:27, Jim Lemon wrote:
>> > > On Tue, 7 Oct 2014 10:32:42 AM Frederic Ntirenganya wrote:
>> > >> Dear All,
>> > >>
>> > >> How can I change the format of date of day of the year ? for
>> > >
>> > > example r
>> > >
>> > >> (i.e. "17 Apr" rather than "108").
>> > >>
>> > >> The following is the type of the dataset I have
>> > >>
>> > >> head(Samaru)
>> > >>
>> > >>    Year Start End Length
>> > >>
>> > >> 1 1930   108 288    180
>> > >> 2 1931   118 288    170
>> > >> 3 1932   115 295    180
>> > >> 4 1933   156 294    138
>> > >> 5 1934   116 291    175
>> > >> 6 1935   134 288    154
>> > >
>> > > Hi Frederic,
>> > > The easiest method I can think of is this:
>> > >
>> > > Samaru$Start<-format(as.Date(
>> > >
>> > >   paste(Samaru$Year,"01-01",sep="-"))+Samaru$Start,
>> > >   "%b %d")
>> > >
>> > > Samaru$End<-format(as.Date(
>> > >
>> > >   paste(Samaru$Year,"01-01",sep="-"))+Samaru$End,
>> > >   "%b %d")
>> >
>> > In the package 'eha' I have a function 'toDate':
>> >  > require(eha)
>> >  > toDate(1930 + 108/365)
>> >
>> > [1] "1930-04-19"
>> >
>> > (Interestingly, we are all wrong; the correct answer seems to be
>> > "1930-04-18")
>> >
>> >  > toDate
>> >
>> > function (times)
>> > {
>> >      if (!is.numeric(times))
>> >          stop("Argument must be numeric")
>> >      times * 365.2425 + as.Date("0000-01-01")
>> > }
>> >
>> > The 'inverse' function is 'toTime'.
>> >
>> > Sometimes it misses by one day; not very important in my
>> applications,
>> > but may be otherwise.
>> >
>> Hi Goran,
>> You're correct.
>>
>>  as.Date("Apr 19 1930","%b %d %Y") -
>> + as.Date("Jan 1 1930","%b %d %Y")
>> Time difference of 108 days
>>
>> The t
>>
>> Samaru$Start<-format(as.Date(
>>  paste(Samaru$Year,"01-01",sep="-"))+Samaru$Start-1,"%b %d")
>> Samaru$End<-format(as.Date(
>>  paste(Samaru$Year,"01-01",sep="-"))+Samaru$End-1, "%b %d")
>>
>> Jim
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Frederic Ntirenganya
> Maseno University,
> Kenya.
> Mobile:(+254)718492836
> Email: fredo at aims.ac.za
> https://sites.google.com/a/aims.ac.za/fredo/
>



-- 
Frederic Ntirenganya
Maseno University,
Kenya.
Mobile:(+254)718492836
Email: fredo at aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

	[[alternative HTML version deleted]]



More information about the R-help mailing list