2013-10-11 1 views
0

나는 매일 시리즈를 금요일에 끝나는 주간 시리즈로 변환하려고합니다. 각 계열에 누락 된 값이 있으므로 merge 함수가 일부 NAs를 남겨 둡니다. na.locfto.weekly으로 시도했지만 나에게 도움이되지 못했습니다. 나는 그 기간에 모든 금요일을 포함하는 주간 날짜 개체를 만들고 있는데 일부 주에는 수요일이나 목요일에 끝나기 때문에 일부 인덱스는 일치하지 않습니다.일일 시리즈를 주간으로 변환하여 실제 날짜와 상관없이 모든 주 금요일을 인덱스로 변경합니다.

이상적으로 나는 금요일에 끝나지 않는 그 주간의 마지막 값의 날짜를 덮어 쓰고 싶습니다. @G에

library(quantmod) 
library(xts) 

TickerL <- c("ABE.MC", "FNC.MI", "ENI.MI") 

getSymbols(TickerL,from = "2000-01-01") 

pr <- list(ABE.MC[,4], FNC.MI[,4], ENI.MI[,4]) 

w.dates <- seq(from=min(index(ABE.MC)),to=max(index(ABE.MC)), by='days') 
w.dates <- w.dates[.indexwday(as.xts(w.dates))==5] 
if (max(index(ABE.MC)) > max(w.dates)) w.dates <- c(w.dates,seq(last(w.dates),by='weeks',length.out=2)[2]) 

pr2 <- lapply(pr, function(x) do.call(rbind, lapply(split(x, "weeks"), last))) 

pr3 <- do.call(merge, pr2) 
+1

동물원 패키지의 빠른 참조 비 네트에서 한 줄 'nextfri' 기능을 참조하십시오. –

+0

@G. Grothendieck 많은 감사합니다! – nopeva

답변

0

감사합니다. 기능을 Grothendieck nextfri 동물원 패키지 비 네트에서 트릭을 했어!

# given a Date, x, return the Date of the next Friday 
nextfri <- function(x) 7 * ceiling(as.numeric(x - 1)/7) + as.Date(1) 

pr2 <- lapply(pr, function(x) x <- do.call(rbind, lapply(split(x, "weeks"), last)) 
        index(x) <- nextfri(index(x)) 
        x 
) 

pr3 <- do.call(merge, pr2)