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

2016-10-31 Thread Bob Rudis
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()

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

2016-10-31 Thread William Dunlap via R-help
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 erro

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

2016-10-31 Thread Bert Gunter
But if all the OP want is a count, wouldn't sum(sapply(buylist, slot, "reqstock") > 100) suffice? -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon

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

2016-10-31 Thread Bob Rudis
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 wrote: > I have the following object > > setClass("buyer", > representation( > reqstock="numeric", > buyout="

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

2016-10-31 Thread Thomas Chesney
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