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

PIKAL Petr petr.pikal at precheza.cz
Tue Nov 4 16:55:22 CET 2014

```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

}

>>   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
>
> 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
> >>
> >>         [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> 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
> 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.
```