Reputation: 11
I would like to write a function that will take date as input argument and output will be day, month, week and week year. My sample code shown some error. Kindly help me in this regards, thank you.
My Sample code as follows:
myFunction <- function(date){
date <-as.numeric(as.Date(date, format = "%m/%d/%Y",origin = "1899-12-30"))
date$month<- strftime(date,"%m")
date$day<- strftime(date,"%d")
data$week<-strftime(date,"%w")
date$week_year<-strftime(date,"%W")
return(date$day,date$month,date$week,date$week_year)
}
When I called function ,It shown error:
myFunction(2016-07-26)
Error in as.POSIXlt.numeric(x, tz = tz) : 'origin' must be supplied
Upvotes: 0
Views: 2238
Reputation: 7063
Your input is a string. Using lubridate
you could write
myFunction <- function(date){
library(lubridate)
t0 <- ymd(date)
return(list(day(t0), month(t0), week(t0), wday(t0, label=F, abbr=F), year(t0)))
}
Upvotes: 1