0

I have some publicly listed companies' financial statements in lists with similar structures, already loaded in Global Environment. I want to select a particular element from each list and rbind them in a separate column.

For this I have first stored the name of the lists in a matrix and then I am dynamically selecting each entity using $ operator while varying the name of the list using for loop.

Example data set:

library(quantmod)
cname <- as.matrix(data.frame(com = c('MCK','CERN','IMS','MDRX','HURN')))
for( i in 1:nrow(cname)){
  sm <- cname[i,1]
  getFinancials(Symbol=sm, src="google")
}

I want to get total revenue data from each company financial statements. For this I am doing the following:

# total revenue 
tr0 <- NULL
for(i in 1:nrow(cname)){
  sm <- cname[i,1]
  com.rev <- paste(sm,'.f$IS$A',sep = '')
  tr <- com.rev["Total Revenue",]
  tr0 <- rbind(tr0,tr)
}

While doing so, I am getting following error:

Error

Error in com.rev["Total Revenue", ] : incorrect number of dimensions

Mithilesh Kumar
  • 256
  • 1
  • 3
  • 18
  • @Batanichek It's typo just corrected that. Still I am facing the same problem. – Mithilesh Kumar Dec 28 '15 at 10:41
  • 1
    `com.rev <- paste(sm,'.f$IS$A',sep = '')` its character string ( my mind). how you want to subset it? – Batanichek Dec 28 '15 at 10:42
  • Yeah, It's a character string, but com name is same as that of list name, I am using it to access the internal elements. The original argument for com 1 data should be: `MCK.f$IS$A["Total Revenue",]` . It works. – Mithilesh Kumar Dec 28 '15 at 10:47
  • 2
    see `eval` `com.rev <- eval(parse(text=paste(sm,'.f$IS$A',sep = '')))` – Batanichek Dec 28 '15 at 10:48
  • 1
    See the duplicate and the use of `assign`. –  Dec 28 '15 at 10:49
  • Thanks @Batanichek ! It solves my problem. – Mithilesh Kumar Dec 28 '15 at 10:53
  • 1
    `assign` also can do it, and i dont know which way better – Batanichek Dec 28 '15 at 10:54
  • 1
    require(quantmod) setwd("C:/Users/downloads/") stocks <- c("AXP","BA","CAT","CSCO","XOM") # equityList <- read.csv("EquityList.csv", header = FALSE, stringsAsFactors = FALSE) # names(equityList) <- c ("Ticker") for (i in 1 : length(stocks)) { temp<-getFinancials(stocks[i],src="google",auto.assign=FALSE) write.csv(temp$IS$A,paste(stocks[i],"_Income_Statement(Annual).csv",sep="")) write.csv(temp$IS$A,paste(stocks[i],"_Income_Statement(Quarterly).csv",sep="")) } –  Jan 06 '16 at 21:35

0 Answers0