r - How should I deal with 'from' must be of length 1 error? -


i tried count days between specific dates. have 2 columns character vectors.

start.date <- c("2015-01-10","2015-01-11","2015-02-24") end.date <- c("2015-03-10","2015-04-01","2015-06-13") date10 <- data.frame(cbind(start.date,end.date)) date10$start.date <- as.character(date10$start.date) date10$end.date <- as.character(date10$end.date) str(date10) 

and specific dates 2015-04-11 2015-07-10. made date between specific dates using seq(). sp.da1<-ymd("2015-04-11") sp.da2<-ymd("2015-07-10") inteval.da<-seq(sp.da1, sp.da2, = 'day') wanted know how many days between specific dates. tried use seq(start.date,end.date,by = 'day') above, error: 'from' must of length 1

please me!!!

you asking how many days of given time interval inside main time interval.

let's first set 3 varying time intervals. write function checks every day x whether inside or outside main interval. if sum number of days inside main interval, we'll have you're searching for:

date10$start.date <- as.date.character(date10$start.date, format="%y-%m-%d") date10$end.date   <- as.date.character(date10$end.date,   format="%y-%m-%d")  your_intervals <- map(seq, = date10[, 1], = date10[, 2], = "days") 

your_intervals list 3 data frames, each containing every day in interval.

is_in_interval <- function(x, l_bound = sp.da1, u_bound = sp.da2){   return (x > l_bound) & (x < u_bound) }  sapply(your_intervals, function(x) sum(is_in_interval(x))) # [1] 0 0 63 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -