2014-01-31 2 views
0

Quandl에 R 패키지를 사용하여 지정된 회사 목록에 대한 주가 정보가있는 데이터 프레임을 만들려고합니다.순차 병합 루프에서 R

install.packages("Quandl") 

library(Quandl) 
library(reshape) 

Quandl.auth("yourauthenticationtoken") 

#create date structure (using AAPL) 
structure <- Quandl("GOOG/NASDAQ_AAPL",start_date="2004-01-01",end_date="2013-12-31", collapse="weekly")[c(1)] 

#list of stocks to fetch 
stocks <- c("MSFT", "AAPL") 

# Function to fetch stock quotes 
rdQcurr <- function(curr){ 
    codes <- paste("GOOG/NASDAQ_",curr,sep="") 
    for(i in 1:length(stocks)){ 
    df<-Quandl(codes[i],start_date="2004-01-01",end_date="2013-12-31", collapse="weekly")[c(1,2)] 

    #rename coloumn 2 to the name of the stock 
    names(df)[2]<-paste(stocks[i]) 
    #merge i'th stock to structure data frame 
    structure <- merge(x=structure, y=df, by = "Date", all.x=TRUE) 
} 
} 

quotes <- rdQcurr(stocks) 

편집 :이 코드는 실행되지만 데이터 프레임 "quotes"는 NULL입니다.

이 문제를 해결하는 방법에 대한 아이디어가 있으십니까?

+1

코드의 기본을 통과하십시오! 함수에서 return (structure)가 원하는 것일 수 있습니다. – Aashu

+0

질문을 편집했습니다. 코드를 실행하는 것이 옳습니다. 그러나 NULL 인 "따옴표"데이터 프레임을 생성합니다. – Sunv

+0

@Aashu'return (structure)'를 어디에 두겠습니까? – Sunv

답변

3

하고있다 R

rdQcurr <- function(curr){ 
    codes <- paste("GOOG/NASDAQ_",curr,sep="") 
    for(i in 1:length(stocks)){ 
    df<-Quandl(codes[i],start_date="2004-01-01",end_date="2013-12-31", collapse="weekly")[c(1,2)] 

    #rename coloumn 2 to the name of the stock 
    names(df)[2]<-paste(stocks[i]) 
    #merge i'th stock to structure data frame 
    structure <- merge(x=structure, y=df, by = "Date", all.x=TRUE) 
} 
return(structure) 
} 
> quotes <- rdQcurr(stocks) 
> quotes 
      Date MSFT 
1 2004-01-04 27.58 
2 2004-01-11 28.03 
3 2004-01-18 27.72 
4 2004-01-25 28.26 
5 2004-02-01 27.84 
---------------------