2014-04-09 2 views
1

R의 동물원 객체를 일별 값에서 주별, 월별 또는 연도별로 축소해야합니다. to.monthly() 등의 접근 방식을 시도했지만 트릭을 수행하지 않는 것 같습니다. 다음과 같이축소 날짜 : R

데이터가 구성되어

  CARL.B.CO.Adjusted COLOB.CO.Adjusted 
2001-03-01    339.70    2.98 
2001-03-02    358.57    3.03 
2001-03-05    360.46    3.02 
2001-03-06    360.46    3.00 
2001-03-07    360.46    3.02 
2001-03-08    360.46    3.02 

이 Yahoo.finance 매일 stockprices를 포함하는 행렬은 QuantMod 패키지를 통해 다운로드, 만 조정 가까운 가격을 사용하여 재 편곡 한 것. 이것은 쉬운 manouvre이어야하지만, 나는 운이없이 꽤 많은 시간을 인터넷 검색에 보냈다.

미리 감사드립니다.

편집 :

dput 출력 : 데이터를 검색에 대한

> dput(head(data)) 
structure(c(339.7, 358.57, 360.46, 360.46, 360.46, 360.46, 2.98, 
3.03, 3.02, 3, 3.02, 3.02), .Dim = c(6L, 2L), .Dimnames = list(
c("2001-03-01", "2001-03-02", "2001-03-05", "2001-03-06", 
"2001-03-07", "2001-03-08"), c("CARL.B.CO.Adjusted", "COLOB.CO.Adjusted" 
)), index = structure(c(11382, 11383, 11386, 11387, 11388, 
11389), class = "Date"), class = "zoo") 

코드 : 나는 to.monthly (시도했습니다

GetData <- function(tickers, startdate, enddate, fillforward = TRUE) 
      { 
       getSymbols(tickers, from=startdate, to = enddate,src="yahoo") 
       if (fillforward == TRUE) 
        { 
         na.locf(zoo(do.call(merge, lapply(tickers, function(x) Ad(get(x)))))) 
        } 
       else 
        { 
         do.call(merge, lapply(tickers, function(x) Ad(get(x)))) 
        } 
      } 

) 여기에 제안한 : https://stat.ethz.ch/pipermail/r-sig-finance/2009q1/003704.html

그러나 하나 이상의 심볼을 검색 할 때 트릭을 수행하지 않습니다. 그 Z 가정

+0

적어도 데이터의 일부분의'dput()'을 게시 할 수 있습니까? 또한 시도한 내용의 일부분을 게시 할 수 있습니까? 심판 : [최소 재현 가능한 예] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – hrbrmstr

+0

편집 - 감사를 참조하십시오! – marcopah

답변

1

1) aggregate.zoo 데이트 클래스 색인이 시도 가지고

library(zoo) 
aggregate(z, f, tail, 1) 
함수 f 이들 중 하나

:

as.year <- function(x) as.numeric(format(x, "%Y")) 

last.date.of.year <- function(x) as.Date(format(x, "%Y-12-31")) 

as.yearmon 

last.date.of.month <- function(x) as.Date(as.yearmon(x), frac = 1) 

nextfri # Date of next Fri or same date if Fri 

nextfri 함수는 하나이다 zoo-quickref vignette에 정의 된 라인 기능.

예 : aggregate(z, nextfri, tail, 1).

2) period.apply 다른 접근법은 xts 패키지에서 period.apply을 사용하는 것입니다. ?period.apply