Francois.Bastardie at ifremer.fr
2006-Apr-20 13:47 UTC
[R] S4 objects with list of objects as slots: how to subset?
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
Gabor Grothendieck
2006-Apr-20 13:59 UTC
[R] S4 objects with list of objects as slots: how to subset?
Try: c(sapply(1:2, function(i) a.class at my.list[[i]]@my.slot)) On 4/20/06, Francois.Bastardie at ifremer.fr <Francois.Bastardie at ifremer.fr> wrote:> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >