[R] Changing date format

Jim Lemon jim at bitwrit.com.au
Tue Oct 7 11:27:57 CEST 2014


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")
Samaru

  Year  Start    End Length
1 1930 Apr 19 Oct 16    180
2 1931 Apr 29 Oct 16    170
3 1932 Apr 25 Oct 22    180
4 1933 Jun 06 Oct 22    138
5 1934 Apr 27 Oct 19    175
6 1935 May 15 Oct 16    154

Jim



More information about the R-help mailing list