Loop R with Quandl as optional

2020-01-19 03:49发布

问题:

I'm starting programming R. I use Quandl to download historical futures data (GCG1975, GCJ1975, GCM1975, GCQ1975, GCV1975, GCZ1975, GCG1976, GCJ1976, GCM1976, ..., GCZ2016).

Month codes:

G J M Q V Z

Years:

1975:2016

I want to download it all, but I don't want to tape it all, so I think I want a function that downloads a year with all the months and then the next year again all the months. As an example, to download the first year:

require(Quandl)
Quandl("CME/GCG1975")

Any tip about the function or functions that are needed to this question is useful for me. Also if someone knows how to do it in Python is fine.

Thanks,

RTA

回答1:

This answer assumes reading of the earlier question and comments so is not "code only".

 qt = expand.grid(Month=c("G","J","M","Q","V"), Year=1975:2016) 
 query_names_vec <- apply(qt, 1, 
                       function(x) paste0("CME/GC", paste0( x, collapse="") ) )

> head( query_names_vec )
[1] "CME/GCG1975" "CME/GCJ1975" "CME/GCM1975" "CME/GCQ1975"
[5] "CME/GCV1975" "CME/GCG1976"