2017-09-12 2 views
0

시간 간격을 반복하고 간격 내에있는 관측치를 표시하고 싶습니다. 또한 간격 길이를 유연하게 설정할 수있는 솔루션을 원합니다. 지금까지, 나는 그럭저럭 sth까지 그럭저럭 할 수 있었다. 같은 :시간 간격으로 반복하십시오.

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) 

내가 더 우아한 버전이 존재 함을 바라지 만 나는 각 배 내에서 복잡한 계산을하여 여러개의 추정을 결합 perfrom 할 필요가 있기 때문에 caret를 사용하지 않을 것을 기억합니다.

+0

예제가 재생성되지 않습니다. –

+0

방금 ​​수정했습니다. –

답변

0

연도와 달 후 바로 비교가 수행 할 수 있습니다 "yearmon" 클래스로 변환하는 경우 : 또한

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 

경우 en입니다 그 다음 2 인 문제로 st 후 년간의 알려진 번호, ## 대신 라인의

en <- st + 2 

는 표시 (그리고 2 년 6 개월 년 개월 알려진 번호, 예를 들면 2 + 6/12 = 2.5이다 유사 경우) : 우리는 그냥 쓸 수 있습니다.

관련 문제