2013-04-13 2 views
5

몇 가지 이벤트 표시기가있는 xts 객체가 있습니다. 주어진 이벤트에서 다음 이벤트까지의 모든 항목이 동일한 xts에 저장되어 결국 xts 개체의 목록을 생성하고 각 이벤트는 다른 항목이없는 마지막 항목으로 포함되도록 특정 이벤트별로 분할해야합니다. 같은 유형의 이벤트.이벤트로 xts 객체 분할하기

예 : 귀하의 제안이 크게 감사합니다

ts = as.Date(Sys.Date()-99:0) 
e1 = numeric(100);e1[10*1:10]=1 
e2 = numeric(100);e2[15*1:6]=1 
y = 1:100 # just a sample content 
xs = as.xts(cbind(e1,e2,y),order.by=ts) 

ee = e1*e2==1 # the event in which both e1 and e2 are 1, should happen at 30,60,90 

# here should be splitting function that gets xs and ee as parameters 
# and should return a list of 4 xts: the first with the entries 1 through 30, 
# the second with entries 31 to 60, the third with entries 61 to 90, and the last 
# with entries 91 to 100 

.

답변

4

cumsum(ee)을 사용하여 그룹화 변수를 만든 다음 split을 호출하십시오. TRUE 값이 그룹의 마지막 관찰 값이되기 (첫 번째 값이 아닌)되기를 원하므로 cumsum의 결과를 약간 변경해야합니다.

split(xs, c(0,head(cumsum(ee),-1))) 
split(xs,rev(cumsum(rev(ee)))) # grouping factors reversed