[R] Function that create day of the year column.

daniel daniel319 at gmail.com
Tue Nov 4 14:29:33 CET 2014


Maybe, you can add a parameter with a list of the column names and inside
the function an if statement, if the list lenght is 1 use the function with
1 column else the 3 columns.  I am sure you can find better solutions.

Daniel Merino

2014-11-04 9:30 GMT-03:00 Frederic Ntirenganya <ntfredo at gmail.com>:

> Hi Daniel,
>
> How can I add an if conditiopn or for loop to implement this in my
> function?
>
> ex: option1 : single date
>      option2: date in 3 columns (year,moth,day)
>
> yday(Sys.Date())
> yday(as.Date(paste(y,m,d,sep="-")))
>
> Regards,
> Frederic.
>
>
> Frederic Ntirenganya
> Maseno University,
> African Maths Initiative,
> Kenya.
> Mobile:(+254)718492836
> Email: fredo at aims.ac.za
> https://sites.google.com/a/aims.ac.za/fredo/
>
> On Tue, Nov 4, 2014 at 3:20 PM, daniel <daniel319 at gmail.com> wrote:
>
>> Frederic,
>>
>> Check the lubridate library.
>>
>> install.packages("lubridate")
>> library(lubridate)
>> yday(Sys.Date())
>> d <- 4
>> m <- 11
>> y <- 2014
>> yday(as.Date(paste(y,m,d,sep="-")))
>>
>> Daniel Merino
>>
>> 2014-11-04 7:01 GMT-03:00 Frederic Ntirenganya <ntfredo at gmail.com>:
>>
>>> Dear All,
>>>
>>> I would like to make a function that create Day of the year column in a
>>> dataset.
>>> State of the problem: write a function that create a day column either
>>> from
>>> a single date column (string/or factors) or from date in 3 columns (year,
>>> month, day).
>>>
>>> I made the following function for a single date. I would like to add a
>>> condition for date in 3 columns (year, month, day). My data is daily
>>> climate data.
>>> #write a function that create a day column either from a single date
>>> column
>>> (string/or factors)
>>> #or from date in 3 columns (year, month, day).
>>>
>>> DOY=function(data){
>>>
>>> #=================================================================
>>> #This function create day of teh year from a single date
>>> column(ex:2009-08-02) or/and
>>> #from the date in 3 columns (Year, month, Day).
>>> #================================================================
>>>   data$Rain=as.numeric(as.character(data$Rain))
>>>   dt=yday(data$Date) # single date of the data
>>>   datelp= dt>59 & !leap_year(data$Date)# tell us that the date occurs
>>> during a leap year
>>>   dt[datelp]=dt[datelp]+1 # add one for non leap_year
>>>   cbind(data, dt) # combining columns of data
>>>   conames(data)="DOY" # name of new column. ??I have a problem on how I
>>> can
>>> precise the column in gerenal.
>>> }
>>>
>>> ex: year  month day   Date         Rain Tmin Tmax
>>>       1971   1         1    1971-01-01   0     8.2  15
>>>        1971  1         2    1971-01-02   0     4.2  11
>>>         .        .          .       .               .      .      .
>>>         .        .          .       .               .      .      .
>>>         .        .          .       .               .      .      .
>>>
>>> Any ideal on how I can make this function is welcome. thanks!
>>> Frederic Ntirenganya
>>> Maseno University,
>>> African Maths Initiative,
>>> Kenya.
>>> Mobile:(+254)718492836
>>> Email: fredo at aims.ac.za
>>> https://sites.google.com/a/aims.ac.za/fredo/
>>>
>>>         [[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.
>>>
>>
>>
>>
>> --
>> Daniel
>>
>
>


-- 
Daniel

	[[alternative HTML version deleted]]



More information about the R-help mailing list