[R] The equivalent of which() when accessing slots in an object

Bob Rudis bob at rud.is
Mon Oct 31 15:31:17 CET 2016


    which(purrr::map_dbl(buylist, slot, "reqstock") > 100)

or

    which(sapply(buylist, slot, "reqstock") > 100)

ought to do the trick.

On Mon, Oct 31, 2016 at 10:09 AM, Thomas Chesney
<Thomas.Chesney at nottingham.ac.uk> wrote:
> I have the following object
>
> setClass("buyer",
> representation(
> reqstock="numeric",
> buyout="numeric"),
> )
>
> nBuy <- 5
>
> #Set buyer parameters here
> buylist <- list()
> for (i in 1:nBuy){
> buylist[[i]] <- new("buyer")
> buylist[[i]]@reqstock <- sample(c(50:200),1)
> }
>
> and want to count the number of objects in buylist that have reqstock greater than 100. Something like this if it was a vector:
>
> length(which(buylist[[]]@reqstock > 100))
>
> How could I do this please and where could I find more information about manipulating slots? When I try to search for this I just keep finding info on ls() and similar functions.
>
> Thank you
>
> Thomas Chesney
> http://www.nottingham.ac.uk/~liztc/Personal/index.html
>
>
>
> This message and any attachment are intended solely for the addressee
> and may contain confidential information. If you have received this
> message in error, please send it back to me, and immediately delete it.
>
> Please do not use, copy or disclose the information contained in this
> message or in any attachment.  Any views or opinions expressed by the
> author of this email do not necessarily reflect the views of the
> University of Nottingham.
>
> This message has been checked for viruses but the contents of an
> attachment may still contain software viruses which could damage your
> computer system, you are advised to perform your own checks. Email
> communications with the University of Nottingham may be monitored as
> permitted by UK legislation.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list