2013-04-13 24 views
5

Birkaç olay göstergeli bir xts nesnesine sahibim. Belirli olaylara göre bölme yapmam gerekiyor, böylece belirli bir olaydan sonraki olaya kadar olan tüm girdiler, xts nesnesinin bir listesini oluşturacak şekilde aynı xts nesnesine kaydedilecek ve her biri olayı içeren son giriş olarak olay içeren aynı tür olaylar.xts nesnesini olaylar ile bölme

bir örnek: Önerileriniz büyük takdir edilecektir

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 

.

cevap

4

Gruplama değişkeninizi oluşturmak için cumsum(ee) kullanın, ardından split numaralı telefonu arayın. TRUE değerlerinin grubun son gözlemi olmasını istediğinizden (cumsum) küçük bir değişiklik yapmanız gerekir (ilk yerine).

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