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

Bob Rudis bob at rud.is
Mon Oct 31 18:08:43 CET 2016


Aye, Bill. I figured others wld include a `vapply()` example (didn't
want to "hog" the answer :-).

I went with bland `sapply()` as an alternative since I made an
assumption Thomas (like the large % of R users I've come in contact
with - albeit a biased sample) aren't really familiar with `vapply()`.

On Mon, Oct 31, 2016 at 12:29 PM, William Dunlap <wdunlap at tibco.com> wrote:
> vapply(buylist, slot, "reqstock", FUN.VALUE=0.0)
> is closer in spirit than sapply() to purrr::map_dbl() - FUN.VALUE
> gives the expected type and size of f's output.  vapply() is usually
> quicker than sapply(), uses less memory, gives the right results
> when given a vector of length 0, and gives an error when FUN does
> not return the specified sort of result.
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Mon, Oct 31, 2016 at 7:31 AM, Bob Rudis <bob at rud.is> wrote:
>>
>>     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.
>>
>> ______________________________________________
>> 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