r - Iterate through time interval -


i want loop through time intervals , mark observations within interval. further, want solution flexible in setting interval length. far, managed to sth. like:

set.seed(1) data=data.frame(start_year=sample(2007:2017,100,true),start_month=sample(1:12,100,true)) window_length=2 month=6  iteration_variable=2 end_horizon=2007+window_length+iteration_variable start_horizon=2007+iteration_variable   data$period=ifelse( (data$start_year<=end_horizon & data$start_month<=month ) & (start_horizon<=data$start_year & month<=data$start_month ),1,0) 

i hope there exists more elegant version, recall, don't want use caret since need combine mutiple estimations , perfrom complex computations within each fold.

if year , month converted "yearmon" class straight comparisons can done:

library(zoo)  to_ym <- function(y, m) as.yearmon(y + (m-1)/12)  ym <- with(data, to_ym(start_horizon, start_month)) st <- to_ym(start_horizon, month) en <- to_ym(end_horizon, month) ##  period <- (ym >= st & ym <= en) + 0 

also if en known number of years after st, in question 2, write:

en <- st + 2 

instead of line marked ## (and analogously if known number of years , months, e.g. 2 + 6/12 = 2.5 2 years , 6 months).


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -