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/
Function that create day of the year column.
9 messages · Frederic Ntirenganya, Daniel, PIKAL Petr +2 more
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]]
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
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]]
Hi
You want a function with input of either single value or a data frame? This is very strange. One option is to make a method for function but it is quite beyond my capabilities and it is used by "professional" developers.
methods(c) or methods(plot)
So you need to set method for your function DOY something like
DOY.single_value and DOY.data.frame
Anyway
Function yday takes some value in time/date format regardless it is single value or a vector, so the problem is how do you want your function to work and more importantly how general you want it. I simple case you need to check if data is single value or data frame.
DOY=function(data){
if (is.data.frame(data)) {
do stuff for data frame} else {
do stuff for single value}
return something
}
Other comments see in line
data$Rain=as.numeric(as.character(data$Rain))
dt=yday(data$Date) # single date of the data
If data is data frame dt shall be vector uf numbers not a single date
datelp= dt>59 & !leap_year(data$Date)# tell us that the date
datelp is (maybe) a logical vector with the same length as dt
occurs
during a leap year dt[datelp]=dt[datelp]+1 # add one for non leap_year
Why you do this?
dt<-1:365 # e.g. length is 365
datelp<-dt>59 # logical vector
dt[datelp]<-dt[datelp]+1 # change all numbers greater than 59
dt
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 61 62 63 64 65 66 67 68 69 70 71 72 73
^^^^^^
now you have day 60 missing
[73] 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
[91] 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
[109] 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
[127] 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
[145] 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
[163] 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
[181] 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
[199] 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
[217] 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
[235] 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
[253] 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
[271] 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
[289] 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
[307] 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
[325] 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
[343] 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
[361] 362 363 364 365 366
But length of dt stays the same
length(dt)
[1] 365
Cheers
Petr
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. }
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Frederic Ntirenganya
Sent: Tuesday, November 04, 2014 1:31 PM
To: daniel
Cc: r-help at r-project.org
Subject: Re: [R] Function that create day of the year column.
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
[[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.
________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Dear PIKAL,
I am using a data frame not a single value. I wanted to add a new column
"DOY" on a dataset. I made it but I want to make it General. i.e. You will
see in the dataset I am using columns names (Year,month and day.) What if I
have this information in other columns names? In this function I will be
pushed to change my columns names which I don't think it is a good idea.
How can I make it in such away that I can use any column's name which
contains the informations I want to use?
In the previous comment this is what I mean saying a single date "single
date column(ex:2009-08-02)"
The following is the function and head-tail of the data after running the
function.
library(lubridate)
DOY=function(data){
attach(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).
#================================================================
if (is.data.frame(data)) { # check if the data is a dataframe
dt1=yday(as.Date(paste(Year, Month, Day,sep="-")))
# check if the year is leap or not.
datelp= dt1>59 & !leap_year(year(as.Date(paste(Year,Month, Day,sep="-"))))
datelp[is.na(datelp)]=FALSE
dt1[datelp]=dt1[datelp]+1 # add one for non leap_year
data2=cbind(data, dt1) # combining columns of data
n<-ncol(data)
colnames(data2)[n+1] = "DOY"
}
data2
}
head(DOY(Kitale))
Year Month Day Rain DOY
1 1979 1 1 0 1
2 1979 1 2 0 2
3 1979 1 3 0 3
4 1979 1 4 0 4
5 1979 1 5 0 5
6 1979 1 6 0 6
tail(DOY(Kitale))
Year Month Day Rain DOY
11315 2009 12 26 40.7 361
11316 2009 12 27 13.6 362
11317 2009 12 28 42.2 363
11318 2009 12 29 53.6 364
11319 2009 12 30 19.8 365
11320 2009 12 31 0.5 366
Thanks for the help.
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 6:55 PM, PIKAL Petr <petr.pikal at precheza.cz> wrote:
Hi
You want a function with input of either single value or a data frame?
This is very strange. One option is to make a method for function but it is
quite beyond my capabilities and it is used by "professional" developers.
methods(c) or methods(plot)
So you need to set method for your function DOY something like
DOY.single_value and DOY.data.frame
Anyway
Function yday takes some value in time/date format regardless it is single
value or a vector, so the problem is how do you want your function to work
and more importantly how general you want it. I simple case you need to
check if data is single value or data frame.
DOY=function(data){
if (is.data.frame(data)) {
do stuff for data frame} else {
do stuff for single value}
return something
}
Other comments see in line
data$Rain=as.numeric(as.character(data$Rain))
dt=yday(data$Date) # single date of the data
If data is data frame dt shall be vector uf numbers not a single date
datelp= dt>59 & !leap_year(data$Date)# tell us that the date
datelp is (maybe) a logical vector with the same length as dt
occurs
during a leap year dt[datelp]=dt[datelp]+1 # add one for non leap_year
Why you do this?
dt<-1:365 # e.g. length is 365
datelp<-dt>59 # logical vector
dt[datelp]<-dt[datelp]+1 # change all numbers greater than 59
dt
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54
[55] 55 56 57 58 59 61 62 63 64 65 66 67 68 69 70 71 72
73
^^^^^^
now you have day 60 missing
[73] 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
91
[91] 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
109
[109] 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
127
[127] 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
145
[145] 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
163
[163] 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
181
[181] 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
199
[199] 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
217
[217] 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
235
[235] 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
253
[253] 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
271
[271] 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
289
[289] 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
307
[307] 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
325
[325] 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
343
[343] 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
361
[361] 362 363 364 365 366
But length of dt stays the same
length(dt)
[1] 365
Cheers
Petr
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. }
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Frederic Ntirenganya
Sent: Tuesday, November 04, 2014 1:31 PM
To: daniel
Cc: r-help at r-project.org
Subject: Re: [R] Function that create day of the year column.
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
[[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.
________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Hi
single date and vector of dates is something quite different. Your bithday is single date, birthdays of country population is vector of dates.
You want to check that some columns in your data are those you want directly inside the function?
Or you just want to be able to select columns on your own when calling the function?
The first option is quite complicated and it would require huge amount of checking.
If you just want use one or another option = 3 columns or 1 column you can do it by adding some parameter to your function.
DOY=function(data, single = FALSE, which = c(1,2,3) {
if (single) {
....
dt1=yday(as.Date(data[,which])) } else {
....
dt1=yday(as.Date(paste(data[,which[1]], data[,which[2]], data[,which[,3]], sep="-")))
}
....
Something like that although I am not sure if it does not restrict input for which to have always three values. If yes you can for single column option use only the first value, specifying required column.
dt1=yday(as.Date(data[,which[1]]))
Cheers
Petr
PS. No HTML post please.
From: Frederic Ntirenganya [mailto:ntfredo at gmail.com]
Sent: Wednesday, November 05, 2014 9:35 AM
To: PIKAL Petr
Cc: r-help at r-project.org
Subject: Re: [R] Function that create day of the year column.
Dear PIKAL,
I am using a data frame not a single value. I wanted to add a new column "DOY" on a dataset. I made it but I want to make it General. i.e. You will see in the dataset I am using columns names (Year,month and day.) What if I have this information in other columns names? In this function I will be pushed to change my columns names which I don't think it is a good idea. How can I make it in such away that I can use any column's name which contains the informations I want to use?
In the previous comment this is what I mean saying a single date "single date column(ex:2009-08-02)"
The following is the function and head-tail of the data after running the function.
library(lubridate)
DOY=function(data){
attach(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).
#================================================================
if (is.data.frame(data)) { # check if the data is a dataframe
dt1=yday(as.Date(paste(Year, Month, Day,sep="-")))
# check if the year is leap or not.
datelp= dt1>59 & !leap_year(year(as.Date(paste(Year,Month, Day,sep="-"))))
datelp[is.na<http://is.na>(datelp)]=FALSE
dt1[datelp]=dt1[datelp]+1 # add one for non leap_year
data2=cbind(data, dt1) # combining columns of data
n<-ncol(data)
colnames(data2)[n+1] = "DOY"
}
data2
}
head(DOY(Kitale))
Year Month Day Rain DOY
1 1979 1 1 0 1
2 1979 1 2 0 2
3 1979 1 3 0 3
4 1979 1 4 0 4
5 1979 1 5 0 5
6 1979 1 6 0 6
tail(DOY(Kitale))
Year Month Day Rain DOY
11315 2009 12 26 40.7 361
11316 2009 12 27 13.6 362
11317 2009 12 28 42.2 363
11318 2009 12 29 53.6 364
11319 2009 12 30 19.8 365
11320 2009 12 31 0.5 366
Thanks for the help.
Regards,
Frederic.
Frederic Ntirenganya
Maseno University,
African Maths Initiative,
Kenya.
Mobile:(+254)718492836
Email: fredo at aims.ac.za<mailto:fredo at aims.ac.za>
https://sites.google.com/a/aims.ac.za/fredo/
On Tue, Nov 4, 2014 at 6:55 PM, PIKAL Petr <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>> wrote:
Hi
You want a function with input of either single value or a data frame? This is very strange. One option is to make a method for function but it is quite beyond my capabilities and it is used by "professional" developers.
methods(c) or methods(plot)
So you need to set method for your function DOY something like
DOY.single_value and DOY.data.frame
Anyway
Function yday takes some value in time/date format regardless it is single value or a vector, so the problem is how do you want your function to work and more importantly how general you want it. I simple case you need to check if data is single value or data frame.
DOY=function(data){
if (is.data.frame(data)) {
do stuff for data frame} else {
do stuff for single value}
return something
}
Other comments see in line
data$Rain=as.numeric(as.character(data$Rain))
dt=yday(data$Date) # single date of the data
If data is data frame dt shall be vector uf numbers not a single date
datelp= dt>59 & !leap_year(data$Date)# tell us that the date
datelp is (maybe) a logical vector with the same length as dt
occurs
during a leap year dt[datelp]=dt[datelp]+1 # add one for non leap_year
Why you do this?
dt<-1:365 # e.g. length is 365
datelp<-dt>59 # logical vector
dt[datelp]<-dt[datelp]+1 # change all numbers greater than 59
dt
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 61 62 63 64 65 66 67 68 69 70 71 72 73
^^^^^^
now you have day 60 missing
[73] 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
[91] 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
[109] 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
[127] 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
[145] 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
[163] 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
[181] 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
[199] 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
[217] 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
[235] 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
[253] 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
[271] 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
[289] 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
[307] 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
[325] 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
[343] 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
[361] 362 363 364 365 366
But length of dt stays the same
length(dt)
[1] 365
Cheers
Petr
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. }
-----Original Message----- From: r-help-bounces at r-project.org<mailto:r-help-bounces at r-project.org> [mailto:r-help-bounces at r-<mailto:r-help-bounces at r-> project.org<http://project.org>] On Behalf Of Frederic Ntirenganya Sent: Tuesday, November 04, 2014 1:31 PM To: daniel Cc: r-help at r-project.org<mailto:r-help at r-project.org> Subject: Re: [R] Function that create day of the year column. 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<mailto: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<mailto: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<mailto: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<mailto:fredo at aims.ac.za>
https://sites.google.com/a/aims.ac.za/fredo/
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org<mailto: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]]
______________________________________________ R-help at r-project.org<mailto: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.
________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient. ________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
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.
Beware of Sys.Date, since it returns GMT, so depending on your local timezone you may be surprised by the result. I prefer to explicitly set the TZ environment variable and use Sys.time to get local time. You can use trunc() to chop off the time part.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On November 5, 2014 4:01:36 PM EST, "MacQueen, Don" <macqueen1 at llnl.gov> wrote:
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.