I am trying to create a simple function that finds the number of days between two dates. However, when I run the function, R gives me an error message saying: unexpected '}' in " }" Here is my function: #create a function that finds the number of days between two dates diffdate<-function(x,y){ z<-unclass(as.Date(x)) z1<-unclass(as.Date(y)) } { return z1-z } [[alternative HTML version deleted]]
As what is going on is completely obvious, I think you need to consult a local programmer to explain it to you -- probably a 15 year old kid will do. Seriously. Doesn't have to know R (other than that a multiline's function's code/body must be enclosed in "{ }." Perhaps reading an R tutorial (maybe "An Intro to R", which ships with R -- have you read it? If not, why not??) might also do. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll On Fri, Dec 12, 2014 at 1:43 PM, Sajjad Jafri <sjafri83 at gmail.com> wrote:> I am trying to create a simple function that finds the number of days > between two dates. However, when I run the function, R gives me an error > message saying: > > > > unexpected '}' in " }" > > Here is my function: > > #create a function that finds the number of days between two dates > diffdate<-function(x,y){ > z<-unclass(as.Date(x)) > z1<-unclass(as.Date(y)) > } > { > return z1-z > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
On Dec 12, 2014, at 1:43 PM, Sajjad Jafri wrote:> I am trying to create a simple function that finds the number of days > between two dates. However, when I run the function, R gives me an error > message saying:What do you mean "when you run the function"? I do not see any calls to the function. I do see an error message but the functions was defined, nonetheless. All you need to do is type `diffdate` to see it. You will probably be surprised.> unexpected '}' in " }" > > Here is my function: > > #create a function that finds the number of days between two dates > diffdate<-function(x,y){ > z<-unclass(as.Date(x)) > z1<-unclass(as.Date(y)) > } > { > return z1-z > } > > [[alternative HTML version deleted]]Read the Posting Guide regarding teh desired format of posts.> ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.David Winsemius Alameda, CA, USA
The body of a function needs to be wrapped in one pair of braces. diffdate<-function(x,y){ z<-unclass(as.Date(x)) z1<-unclass(as.Date(y)) return(z1-z) } It is common in R to make sure that date values are of the appropriate type before you call the function instead of doing it over and over in this and various other functions you might use. If you give the function Date values instead of character strings you can shorten your function because you can do Date arithmetic directly on them. You also don't need the return statement since you are at the end of the code block already. diffdate2 <- function(x,y){ as.numeric(y-x) } in1 <- "2014-12-12" in2 <- "2014-10-12" in1 <- as.Date(in1) in2 <- as.Date(in2) diffdate2(in1,in2) The expectation that specific types of data will be used is the reason that we are so picky on this mailing list about getting sample data along with sample code... so we know all the pieces of the puzzle that might be giving you trouble. --------------------------------------------------------------------------- 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 December 12, 2014 1:43:13 PM PST, Sajjad Jafri <sjafri83 at gmail.com> wrote:>I am trying to create a simple function that finds the number of days >between two dates. However, when I run the function, R gives me an >error >message saying: > > > >unexpected '}' in " }" > >Here is my function: > >#create a function that finds the number of days between two dates >diffdate<-function(x,y){ > z<-unclass(as.Date(x)) > z1<-unclass(as.Date(y)) > } > { > return z1-z > } > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.