简体   繁体   中英

R - Passing dates as argument of a function

I want to select certain dates from a data.frame and do some operations on them. Argument of my functions is a date I need to subset a column of my data.frame. When I directly write this date in the function, it works. When I pass the date as an argument of the function I get the following error.

This is my function:

move.in.Vs.booking.request <-function(date1="2016-03-10"){

       Bookings <- read.csv( "bookings.csv", header= TRUE, sep=",")

         begin <- date1
         class(begin)<- "date"
         print(begin) #control to see if it get "begin" as a date
         Bookings$Move.in.date  <- Bookings$Move.in.date  
         Bookings$Move.in.date  <- as.Date(Bookings$Move.in.date )

         bk <- Bookings[, c("Move.in.date", "Date.of.booking.request")]
         move.in.date.period <- bk[bk$Move.in.date>= "begin" & bk$Move.in.date <= "2016-04-10", ] 

            booking.request <- as.Date(move.in.date.period$Date.of.booking.request)  

               booking.request <- month(booking.request)
               month.request <- count(booking.request, vars = NULL, wt_var = NULL)

                 print(month.request)
             }

I call the function using the default argument value.

 move.in.Vs.booking.request()

I get this error:

enter code here`[1] "2016-03-10"
attr(,"class")
[1] "date"
 Show Traceback

 Rerun with Debug
 Error in charToDate(x) : 
  character string is not in a standard unambiguous format 

Thank you guys. My mistake was banal. No apostrophes around "begin" is needed in this row:

move.in.date.period <- bk[bk$Move.in.date>= "begin" & bk$Move.in.date <= "2016-04-10", ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM