Use auto.assign = FALSE in your getFinancials() call, and use viewFinancials() to extract the data you want.
items <- c("Cash & Equivalents", "Short Term Investments", "Cash and Short Term Investments") tickers <- c("AAPL", "IBM", "MSFT") for (ticker in tickers) { Data <- getFinancials(ticker, auto.assign = FALSE) HoldQuart <- viewFinancials(Data, "BS", "Q") CashHold <- subset(HoldQuart,rownames(HoldQuart) %in% items) CashT <- t(CashHold) Cashdf <- data.frame(CashT) Cashdf$tic <- ticker assign(paste0(ticker, ".c"), Cashdf) } If you want to continue processing the data in each [ticker].c object, it would be better to put the body of the loop into a function and call lapply(tickers, myfunction). Then you can use lapply() on the result to continue applying functions to the data. You might also be interested in the stackFinancials function I wrote about: http://blog.fosstrading.com/2017/02/stack-financials.html Best, Josh On Wed, Aug 2, 2017 at 10:00 AM, Sparks, John James <jspa...@uic.edu> wrote: > Dear R Helpers, > > I have run into a problem trying to perform a number of actions on a set > of quantmod data objects through a loop and I am hoping that this is an > easy problem for someone else as opposed to very difficult for me. > > The example task is to get the first three objects of the quarterly > balance sheet for a number of companies from the getFinancials object and > put them together into a single file. I can do this one by one, but if I > try to build a loop and use the get function then the results are not > anticipated and leave me baffled. > > If I do it one at a time all is good. > > > require(quantmod) > > getFinancials("AAPL") > getFinancials("IBM") > getFinancials("MSFT") > > > items=c("Cash & Equivalents","Short Term Investments","Cash and Short Term > Investments") > > HoldQuart<-AAPL.f$BS$Q > CashHold<-subset(HoldQuart,rownames(HoldQuart) %in% items) > CashT<-t(CashHold) > Cashdf<-data.frame(CashT) > Cashdf$tic<-"AAPL" > AAPL.c<-Cashdf > > HoldQuart<-IBM.f$BS$Q > CashHold<-subset(HoldQuart,rownames(HoldQuart) %in% items) > CashT<-t(CashHold) > Cashdf<-data.frame(CashT) > Cashdf$tic<-"IBM" > IBM.c<-Cashdf > > > HoldQuart<-MSFT.f$BS$Q > CashHold<-subset(HoldQuart,rownames(HoldQuart) %in% items) > CashT<-t(CashHold) > Cashdf<-data.frame(CashT) > Cashdf$tic<-"MSFT" > MSFT.c<-Cashdf > > > BigCash<-rbind(AAPL.c, IBM.c, MSFT.c) > #setwd<-("C:/Users/HP USER/Documents") > #write.csv(BigCash,file="CashList.csv") > > > When I try to process through this using a loop, however, things go south > pretty quickly. > > tickerlist<-ls(pattern="^[A-Z]+\\.f") > > for( i in 1:1) > { > test<-get(paste0(tickerlist[i],"$BS$Q")) > } > > Error in get(paste0(tickerlist[i], "$BS$Q")) : > object 'AAPL.f$BS$Q' not found > > So I tried to break it up into smaller steps, but the resulting matrix > seems to have lost its structure (see below). > > If someone could help me out, I sure would appreciate. > > Thanks. > --John Sparks > > > tickerlist<-ls(pattern="^[A-Z]+\\.f") > for( i in 1:1) > { > HoldFin<-get(tickerlist[i]) > BSQ<-as.matrix(paste0(HoldFin,"$BS$Q")) > } > BSQ > > [1,] "list(Q = c(52896, NA, 52896, 32305, 20591, 3718, 2776, NA, NA, NA, > NA, 38799, 14097, NA, NA, -165, 14684, 11029, NA, NA, 11029, NA, NA, NA, > 11029, NA, 11029, 11029, NA, NA, NA, NA, 5261.69, 2.1, NA, 0.57, NA, NA, > NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2.1, 78351, NA, 78351, 48175, > 30176, 3946, 2871, NA, NA, NA, NA, 54992, 23359, NA, NA, 122, 24180, > 17891, NA, NA, 17891, NA, NA, NA, 17891, NA, 17891, 17891, NA, NA, NA, NA, > 5327.99, 3.36, NA, 0.57, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, > 3.36, \n46852, NA, 46852, 29039, 17813, 3482, 2570, NA, NA, NA, NA, 35091, > 11761, NA, NA, -159, 12188, 9014, NA, NA, 9014, NA, NA, NA, 9014, NA, > 9014, 9014, NA, NA, NA, NA, 5393.33, 1.67, NA, 0.57, NA, NA, NA, NA, NA, > NA, NA, NA, NA, NA, NA, NA, 1.67, 42358, NA, 42358, 26252, 16106, 3441, > 2560, NA, NA, NA, NA, 32253, 10105, NA, NA, -263, 10469, 7796, NA, NA, > 7796, NA, NA, NA, 7796, NA, 7796, 7796, NA, NA, NA, NA, 5472.78, 1.42, NA, > 0.57, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1.42, 50557, NA, > 50557, \n30636, 19921, 3423, 2511, NA, NA, NA, NA, 36570, 13987, NA, NA, > -510, 14142, 10516, NA, NA, 10516, NA, NA, NA, 10516, NA, 10516, 10516, > NA, NA, NA, NA, 5540.89, 1.9, NA, 0.52, NA, NA, NA, NA, NA, NA, NA, NA, > NA, NA, NA, NA, 1.9), A = c(215639, NA, 215639, 131376, 84263, 14194, > 10045, NA, NA, NA, NA, 155615, 60024, NA, NA, -1195, 61372, 45687, NA, NA, > 45687, NA, NA, NA, 45687, NA, 45687, 45687, NA, NA, NA, NA, 5500.28, 8.31, > NA, 2.18, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 8.31, 233715, > NA, \n233715, 140089, 93626, 14329, 8067, NA, NA, NA, NA, 162485, 71230, > NA, NA, -903, 72515, 53394, NA, NA, 53394, NA, NA, NA, 53394, NA, 53394, > 53394, NA, NA, NA, NA, 5793.07, 9.22, NA, 1.98, NA, NA, NA, NA, NA, NA, > NA, NA, NA, NA, NA, NA, 9.22, 182795, NA, 182795, 112258, 70537, 11993, > 6041, NA, NA, NA, NA, 130292, 52503, NA, NA, -311, 53483, 39510, NA, NA, > 39510, NA, NA, NA, 39510, NA, 39510, 39510, NA, NA, NA, 0, 6122.66, 6.45, > NA, 1.81, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.45, 170910, > \nNA, 170910, 106606, 64304, 10830, 4475, NA, NA, NA, NA, 121911, 48999, > NA, NA, -24, 50155, 37037, NA, NA, 37037, NA, NA, NA, 37037, NA, 37037, > 37037, NA, NA, NA, 0, 6521.5, 5.68, NA, 1.63, NA, NA, NA, NA, NA, NA, NA, > NA, NA, NA, NA, NA, 5.68))$BS$Q" > [2,] "list(Q = c(NA, 59501, 67101, 11579, NA, 20612, 2910, NA, 11367, > 101990, 65124, -37961, 5473, 2617, 189740, 7549, 334532, 28573, 21665, > 9992, 3999, 9113, 73342, 84531, NA, 84531, 98522, 28226, NA, 14351, > 200450, NA, NA, 33579, NA, 100925, NA, -902, 134082, 334532, NA, 5205.81, > NA, 51093, 60452, 14057, NA, 27977, 2712, NA, 12191, 103332, 62759, > -36249, 5423, 2848, 185638, 7390, 331141, 38510, 21895, 10493, 3499, 9733, > 84130, 73557, NA, 73557, 87549, 26948, NA, 14116, 198751, NA, NA, 32144, > NA, 100001, \nNA, -1567, 132390, 331141, NA, 5255.42, NA, 58554, 67155, > 15754, NA, 29299, 2132, NA, 8283, 106869, 61245, -34235, 5414, 3206, > 170430, 8757, 321686, 37294, 20951, 8105, 3500, 9156, 79006, 75427, NA, > 75427, 87032, 26019, NA, 12985, 193437, NA, NA, 31251, NA, 96364, NA, 596, > 128249, 321686, NA, 5336.17, NA, 52638, 61756, 11714, NA, 19042, 1831, NA, > 11132, 93761, 57991, -32543, 5261, 3506, 169764, 7862, 305602, 26318, > 19632, 12496, 3500, 9540, 71486, 68939, NA, 68939, 84935, 24560, NA, > 14076, 179061, \nNA, NA, 30106, NA, 96542, NA, -879, 126541, 305602, NA, > 5393.16, NA, 45084, 55283, 12229, NA, 19824, 2281, NA, 10204, 87592, > 54051, -30848, 5249, 3843, 177645, 7745, 305277, 25098, 21896, 7998, 2500, > 10773, 68265, 69374, NA, 69374, 79872, 22955, NA, 14226, 174820, NA, NA, > 29484, NA, 102021, NA, -956, 130457, 305277, NA, 5478.45), A = c(NA, > 58554, 67155, 15754, NA, 29299, 2132, NA, 8283, 106869, 61245, -34235, > 5414, 3206, 170430, 8757, 321686, 37294, 20951, 8105, 3500, 9156, 79006, > 75427, NA, 75427, \n87032, 26019, NA, 12985, 193437, NA, NA, 31251, NA, > 96364, NA, 596, 128249, 321686, NA, 5336.17, NA, 30212, 41601, 16849, NA, > 30343, 2349, NA, 15085, 89378, 49257, -26786, 5116, 3893, 164065, 5422, > 290345, 35490, 24169, 8499, 2500, 9952, 80610, 53329, NA, 53329, 64328, > 24062, NA, 12989, 170990, NA, NA, 27416, NA, 92284, NA, -1117, 119355, > 290345, NA, 5578.75, NA, 14845, 25077, 17460, NA, 27219, 2111, NA, 14124, > 68531, 39015, -18391, 4616, 4142, 130162, 3764, 231839, 30196, 7689, 6308, > NA, 19255, \n63448, 28987, NA, 28987, 35295, 20259, NA, 7598, 120292, NA, > NA, 23313, NA, 87152, NA, -282, 111547, 231839, NA, 5866.16, NA, 31841, > 40546, 13102, NA, 20641, 1764, NA, 10335, 73286, 28519, -11922, 1577, > 4179, 106215, 5146, 207000, 22367, 4782, 0, NA, 16509, 43658, 16960, NA, > 16960, 16960, 16489, NA, 6344, 83451, NA, NA, 19764, NA, 104256, NA, -296, > 123549, 207000, NA, 6294.37))$BS$Q" > [3,] "list(Q = c(28920, 5319, > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2017 | www.rinfinance.com ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.