Error when passing argument to function to create sequence of dates in R -


i'm trying create function takes dates of dataframe, , creates sequence based on min , max of date column dataframe:

seqgen <- function(date,data){   sequ <- data.frame(seq(min(as.date(date), max(as.date(date)), "day" )))   return(sequ) } 

however returns error

do not know how convert 'x' class “posixlt”

if outside of function :

sequ <- data.frame(seq(min(as.date(df$date)), max(as.date(df$date)), "day")) 

it works fine , creates "sequ" data.frame.

the argument formatted date type.

df:

structure(list(date = structure(c(16526, 16556, 16587, 16617,  16648, 16679, 16709, 16740, 16770, 16801, 16832, 16861), class = "date"),      aggregator.spend..incl.vat. = c(60902, 69361, 62905, 57568,      49662, 47222, 53818, 50798, 40480, 61323, 54975, 56608),      affiliate.spend..incl.vat. = c(108508, 104398, 102589, 88613,      78514, 72745, 84362, 86809, 91729, 110143, 99682, 107421)), .names = c("date",  "aggregator.spend..incl.vat.", "affiliate.spend..incl.vat."), row.names = c(na,  -12l), class = "data.frame") 

slightly modifying function seqgen:

seqgen <- function(data){   out <- with(data, seq(min(as.date(date)), max(as.date(date)), "day" ))   sequ <- data.frame(date = out)   return(sequ) } 

out <- with(data, seq(min(as.date(date)), max(as.date(date)), "day")) uses data.frame "data", i.e. input of function seqgen through function with , read column "date" create sequence. with, don't need specify data$date in seq.

sequ <- data.frame(date = out) write result of seq data.frame "sequ", adding "date" column name.

return(sequ) returns "sequ" data.frame outside environment of function

head(seqgen(df)) #         date # 1 2015-04-01 # 2 2015-04-02 # 3 2015-04-03 # 4 2015-04-04 # 5 2015-04-05 # 6 2015-04-06  tail(seqgen(df)) #           date # 331 2016-02-25 # 332 2016-02-26 # 333 2016-02-27 # 334 2016-02-28 # 335 2016-02-29 # 336 2016-03-01 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -