[R] S4 objects with list of objects as slots: how to subset?

Francois.Bastardie at ifremer.fr Francois.Bastardie at ifremer.fr
Thu Apr 20 15:47:06 CEST 2006


Hello,

I don't manage to see if you have already focussed on this point in some 
previous messages so I post my question:

I have a little problem with the S4 style of programming.
I tried to formalize my question: please consider the following example 
that you can run I think:

#------------------
setClass("my.class1",
     representation(
         my.list        = "list"),
     prototype=prototype(
         my.list        =list()))

#------------------
setClass("my.class2",
     representation(
         my.slot        = "numeric"),
     prototype=prototype(
         my.slot        =as.numeric(1:10)))

#------------------
a.class <- new("my.class1")
a.class at my.list[[1]] <- new("my.class2")
a.class at my.list[[2]] <- new("my.class2")


using indexation, using my class definitions, how could I avoid the 
following FOR-loop (for performance purpose if it changes something) to 
get straightforward for example result = c(1:10,1:10) ?

result <-NULL
for (i in 1:length(a.class at my.list))
    {
    result <- c(result, a.class at my.list[[i]]@my.slot)
    }


this following code doesn't work:
a.class at my.list[1:2]@my.slot


I hope I have been clear,
Thank you,
Regards,

-- 
--------------------------------------------------------
François Bastardie

French Research Institute for the Development of the Sea (IFREMER)
Ecologie et Modèles pour l'Halieutique
Rue de l'Ile d'Yeu
BP 21105
44311 NANTES Cedex 03 - France
Tél : 02 40 37 41 64
Fax : 02 40 37 40 75
E-mail : francois.bastardie at ifremer.fr




More information about the R-help mailing list