Dear Federic

You need to do something like

function(p, weights = NULL, data = NULL, subset = NULL, na.action =
na.fail)  { # but you will have Day, Month, Year, data
#  instead of the first three
   if(is.null(data)) data <- sys.frame(sys.parent())
   mf <- match.call()
   mf$data <- NULL
   mf$subset <- NULL
   mf$na.action <- NULL
   mf[[1]] <- as.name("data.frame")
   mf <- eval(mf, data)
   if(!is.null(subset)) mf <- mf[subset,]
   mf <- na.action(mf)
   p <- as.numeric(mf$p) # here you will have Day <- mf$Day and so on
   weights <- mf$weights

After this code (stolen from somewhere, it is definitely not original to
me) you will find that the variables you need (in my case p and weights)
have been found from data. So if your variables happen to be called
jour, mois and an you will then be able to write Day = jour, Month =
mois, Year = an, data = mesdonnees and have them picked up from your
data frame mesdonnees.

You do not need to attach data in this system and in general it is a bad
idea.


On 05/11/2014 08:34, Frederic Ntirenganya wrote:
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: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

On Tue, Nov 4, 2014 at 6:55 PM, PIKAL Petr <petr.pi...@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-boun...@r-project.org [mailto:r-help-bounces@r-
project.org] On Behalf Of Frederic Ntirenganya
Sent: Tuesday, November 04, 2014 1:31 PM
To: daniel
Cc: r-help@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: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

On Tue, Nov 4, 2014 at 3:20 PM, daniel <daniel...@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 <ntfr...@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: fr...@aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

         [[alternative HTML version deleted]]

______________________________________________
R-help@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@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.


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.


-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2015.0.5557 / Virus Database: 4189/8514 - Release Date: 11/05/14


--
Michael
http://www.dewey.myzen.co.uk/home.html

______________________________________________
R-help@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.

Reply via email to