I have this code:
IEF <- to.monthly(IEF, indexAt="endof")
SPY <- to.monthly(SPY, indexAt="endof")
I would like to use a for loop instead of separate entries,
so the only code that needs to be modified is the list
of symbols.
symbols <- c("IEF", "SPY")
for(symbol in symbols) {
symbol <- to.monthly(symbol, indexAt="endof")
}
This for loop doesn't work. It puts each output into
*symbol *not into *IEF *and *SPY*.
How do I put the output into the existing objects using
a for loop?
Note: to.monthly() is an xts function
--
View this message in context: http://r.789695.n4.nabble.com/For-loop-question-tp4649215.html
Sent from the R help mailing list archive at Nabble.com.
For loop question
4 messages · dae, David Winsemius
On Nov 10, 2012, at 2:36 PM, dae wrote:
I have this code:
IEF <- to.monthly(IEF, indexAt="endof")
SPY <- to.monthly(SPY, indexAt="endof")
I would like to use a for loop instead of separate entries,
so the only code that needs to be modified is the list
of symbols.
symbols <- c("IEF", "SPY")
for(symbol in symbols) {
symbol <- to.monthly(symbol, indexAt="endof")
}
This for loop doesn't work. It puts each output into
*symbol *not into *IEF *and *SPY*.
How do I put the output into the existing objects using
a for loop?
http://127.0.0.1:21620/doc/manual/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f OOOPs. That link is the one to my own private version of the FAQ. You, of course, have one on your machine since it comes with every default installation, or you can get the one oneCRAN from Google with a search on : R FAQ.
Note: to.monthly() is an xts function
David Winsemius, MD Alameda, CA, USA
Thanks. That got me the answer. This works:
symbols = c("IEF","SPY")
getSymbols(symbols)
for(symbol in symbols) {
assign(symbol, to.monthly(get(symbol), indexAt="endof"))
}
#end
--
View this message in context: http://r.789695.n4.nabble.com/For-loop-question-tp4649215p4649227.html
Sent from the R help mailing list archive at Nabble.com.
On Nov 10, 2012, at 10:17 PM, dae wrote:
Thanks. That got me the answer.
Good. That's what the FAQ is for. You should also read the Posting Guide where the reasons behind the request to include context for replies is laid out.
This works:
symbols = c("IEF","SPY")
getSymbols(symbols)
for(symbol in symbols) {
assign(symbol, to.monthly(get(symbol), indexAt="endof"))
}
#end
--
View this message in context: http://r.789695.n4.nabble.com/For-loop-question-tp4649215p4649227.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD Alameda, CA, USA