[R] accessing info in object slots from listed objects using loops

torgrims torgrims at stud.ntnu.no
Wed Mar 17 11:11:40 CET 2010


I have stacked a couple of garchFit objects in a list with names $fit1,
$fit2, ..., $fiti assigning objects names using a loop, i.e. after running
the loop modelStack = list($fit1, $fit2,...,$fiti).

Thus the following apply;
a = modelStack$fit2, then a is the second garchFit object of formal class
'fGarch' with 11 slots, @call, @formula... etc.

I then want to extract information in the 11 slots using another for loop,
say i want to access the slot a at fit$coef. In order for this to happen I need
to combine modelStack$fit2, where the $fit2 object name must be constructed
in the loop, with the slot name @fit$coef.

In order to construct the proper list names one could use the following in
the for loop;
name = paste("modelStack$fit",i,"@fit$coef",sep="")  #
"modelStack$fit2 at fit$coef" for i = 2, 

and then use something like 

But this returns; Error in get(name) : object 'modelStack$fit2 at fit$coef' not

If I just type modelStack$fit2 at fit$coef in the command window it returns the
proper info from the object list. I figure this is because R somehow
interpret "$" and "@" in get() differently than $ and @ as "list

Does anyone know how to extract information in slots of listed objects using
a loop and on the run generated variable/object names?


sample code
	nAssets = length(modelStack)

	for(i in 1:nAssets){
		name = paste("modelStack[",i,"]@name$series$h",sep="")            
		a = get(name)
                      t = length(a)
	} # end for loop

View this message in context: http://n4.nabble.com/accessing-info-in-object-slots-from-listed-objects-using-loops-tp1596135p1596135.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list