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:<jdnew...@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 <sjafr...@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@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.

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

Reply via email to