[R] Trouble with Paste and Quotes and List Objects

peter dalgaard pdalgd at gmail.com
Sat Jun 18 23:45:12 CEST 2011


On Jun 18, 2011, at 23:05 , Sparks, John James wrote:

> Dear R Helpers,
> 
> I have a list that contains a number of objects, each of them financial
> statement data from quantmod (although I don't think that knowledge of
> quantmod is necessary to help with this problem).
> 
>> str(listfinobj)
> chr [1:4815] "A.f" "AA.f" "AACC.f" "AAME.f" "AAN.f" "AAON.f" "AAP.f"
> "AAPL.f" "AAT.f" "AATI.f" "AAU.f" ...
> 
> I can easily pick out the 3rd object in this list.
>> listfinobj[[3]]
> [1] "AACC.f"
> 
> Each of the .f objects has a mildly complicated structure (partial results
> shown below).
>> str(AACC.f)
> List of 3
> $ IS:List of 2
>  ..$ Q: num [1:49, 1:5] 50.4 NA 50.4 NA 50.4 ...
>  .. ..- attr(*, "dimnames")=List of 2
>  .. .. ..$ : chr [1:49] "Revenue" "Other Revenue, Total" "Total Revenue"
> "Cost of Revenue, Total" ...
>  .. .. ..$ : chr [1:5] "2011-03-31" "2010-12-31" "2010-09-30"
> "2010-06-30" ...
>  .. ..- attr(*, "col_desc")= chr [1:5] "3 months ending 2011-03-31" "3
> months ending 2010-12-31" "3 months ending 2010-09-30" "3 months ending
> 2010-06-30" ...
>  ..$ A: num [1:49, 1:4] 198 NA 198 NA 198 ...
>  .. ..- attr(*, "dimnames")=List of 2
>  .. .. ..$ : chr [1:49] "Revenue" "Other Revenue, Total" "Total Revenue"
> "Cost of Revenue, Total" ...
>  .. .. ..$ : chr [1:4] "2010-12-31" "2009-12-31" "2008-12-31" "2007-12-31"
>  .. ..- attr(*, "col_desc")= chr [1:4] "12 months ending 2010-12-31" "12
> months ending 2009-12-31" "12 months ending 2008-12-31" "12 months
> ending 2007-12-31"
> $ BS:List of 2
>  ..$ Q: num [1:42, 1:5] NA NA 6.53 326.25 NA ...
> ....
> 
> I can get the column names for one of the sub-objects of this object.
>> colnames(AACC.f$IS$A)
> [1] "2010-12-31" "2009-12-31" "2008-12-31" "2007-12-31"
> 
> Thanks for your patience so far; here's the question.
> 
> I want to get the column names from all the sub objects in each of the .f
> objects, so I want to build a loop, but I need to be able to refer to the
> column names of the sub object dynamically.  My many attempts with paste
> and get have not worked, I believe because of the quotes and the $'s.  For
> example
> 
>> temp<-colnames(paste(listfinobj[[3]],$BS$A)[1],sep=",")
> Error: unexpected '$' in "temp<-colnames(paste(listfinobj[[3]],$"
> 
>> as.name(paste(as.name(listfinobj[[3]]),as.name("$BS$A"),sep=""))
> `AACC.f$BS$A`
>> colnames(as.name(paste(as.name(listfinobj[[3]]),as.name("$BS$A"),sep="")))
> NULL
>> as.factor(paste(as.name(listfinobj[[3]]),as.name("$BS$A"),sep=""))
> [1] AACC.f$BS$A
> Levels: AACC.f$BS$A
>> colnames(as.factor(paste(as.name(listfinobj[[3]]),as.name("$BS$A"),sep="")))
> NULL
> 
> Please help me to understand how to refer to the column names in the
> sub-objects of the objects in the list dynamically so that I can build a
> loop to get at each of them.
> 

You're not too hot on the "reproducible example" bit, but maybe this works?

colnames(get(listfinobj[[3]])$BS$A)

"BS" or "IS" by the way?


-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list