torgrims
2010-Mar-17 10:11 UTC
[R] accessing info in object slots from listed objects using loops
Hey,
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
get(name)
But this returns; Error in get(name) : object 'modelStack$fit2 at
fit$coef' not
found
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
separators".
Does anyone know how to extract information in slots of listed objects using
a loop and on the run generated variable/object names?
JT
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.
Henrique Dallazuanna
2010-Mar-17 13:06 UTC
[R] accessing info in object slots from listed objects using loops
Try this: sapply(modelStack, slot, 'fit')['coef',] On Wed, Mar 17, 2010 at 7:11 AM, torgrims <torgrims at stud.ntnu.no> wrote:> > Hey, > > 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 > get(name) > > But this returns; Error in get(name) : object 'modelStack$fit2 at fit$coef' not > found > > 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 > separators". > > Does anyone know how to extract information in slots of listed objects using > a loop and on the run generated variable/object names? > > JT > > 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. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Apparently Analagous Threads
- How do I access class slots from C?
- strange strsplit gsub problem 0 is this a bug or a string length limitation?
- Fix for bug in arima function
- slope coefficient of a quadratic regression bootstrap
- Different results of coefficients by packages penalized and glmnet