R语言新手求助:将Quandl股票代码DataFrame转为可用列表
Hey there! Let's break down what's going on with your code and fix it so you can batch pull stock data smoothly.
The Problem with Your Current List Conversion
When you run x2 <- as.list(x1) on your single-column DataFrame x1, you're not getting a list of individual tickers—instead, you're creating a list with one element that contains the entire ticker column as a vector. That's why your subsequent batch request isn't working as expected.
Step 1: Get the Tickers in the Right Format
First, extract the ticker column as a character vector (this is the most useful format for most API batch requests):
# Pull the ticker-only DataFrame x1 <- Quandl.datatable('SHARADAR/SF1', paginate=TRUE, qopts.columns=c('ticker')) # Extract tickers as a character vector tickers <- x1$ticker
If you specifically need a list where each element is a single ticker (though vectors are usually more efficient here), you can convert the vector to a list directly:
tickers_list <- as.list(tickers)
Step 2: Batch Pull Data with the Tickers
Quandl's datatable function accepts a vector of tickers directly in the ticker parameter, so you can use your tickers vector to fetch all the data you need in one go (or in batches if you have a huge number of tickers):
Option 1: Single Batch Request (for smaller ticker sets)
x3 <- Quandl.datatable( 'SHARADAR/SF1', paginate=TRUE, qopts.columns=c('ticker','dimension','datekey','revenue'), ticker = tickers # Pass the ticker vector here )
Option 2: Batched Requests (for large ticker sets)
If you have hundreds or thousands of tickers, a single request might hit API rate limits or size restrictions. Split your tickers into smaller groups and loop through them:
# Split tickers into groups of 50 (adjust the number based on API limits) ticker_groups <- split(tickers, ceiling(seq_along(tickers)/50)) # Fetch data for each group and store in a list batch_data <- lapply(ticker_groups, function(group) { Quandl.datatable( 'SHARADAR/SF1', paginate=TRUE, qopts.columns=c('ticker','dimension','datekey','revenue'), ticker = group ) }) # Combine all batch results into one DataFrame x3 <- do.call(rbind, batch_data)
Quick Recap
as.list(x1)converts the columns of your DataFrame into list elements, not the individual rows/tickers.- Always extract the
tickercolumn first withx1$tickerto get a usable vector/list. - Use batched requests if you're working with a large number of tickers to avoid API issues.
内容的提问来源于stack exchange,提问作者Dan

