Frederic Ntirenganya
2014-Nov-04 10:01 UTC
[R] Function that create day of the year column.
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]]
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
[[alternative HTML version deleted]]
I would start with this example, which is available from base R, without additional packages, to help understand the suggestions that follow.> unclass(as.POSIXlt(Sys.Date()))$sec [1] 0 $min [1] 0 $hour [1] 0 $mday [1] 5 $mon [1] 10 $year [1] 114 $wday [1] 3 $yday [1] 308 $isdst [1] 0 attr(,"tzone") [1] "UTC" And then see the $yday element For example:> as.POSIXlt( as.Date('2014-9-13') )$yday[1] 255> as.POSIXlt( as.Date('2014-1-1') )$yday[1] 0 Note that the year starts with day 0, which might not be what you expect. If you have three columns try as.POSIXlt( as.Date( paste(year, month, day, sep='-') )$yday Which can be illustrated by> as.POSIXlt( as.Date( paste(2014, 1, 31, sep='-') ) )$yday[1] 30 If your ?date? column is already of class Date > class(Sys.Date()) [1] "Date" then as.POSIXlt( date )$yday is sufficient. Otherwise you have to convert it to Date class. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 11/4/14, 2:01 AM, "Frederic Ntirenganya" <ntfredo at gmail.com> wrote:>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.