Description Usage Arguments Details Value Examples
As S3 generic to create a subclass of IterateR
.
Object that are subclasses of IterateR
have
numberous reference class methods defined for them,
though the primary one is each
.
1 |
x |
object to turn into an iterable object. |
mode |
If |
... |
passed to |
The each
method allows a simple means to iterate
over the objects. The functionality is very similar to
sapply
, say, but has some novelties: the function
arguments determine what gets passed in. If the function
has arguments x
, i
or nm
then these
are passed the object, its index, or name, respectively.
Other methods include:
split
to split the objects before iterating
filter
to apply a function to filter the objects
by
find
to apply R's Find
function
position
to apply R's Position
function
tail
to call tail
head
to call head
slice
to call [
Each of the above returns an instance of a subclass of
IterateR
allowing them to be chained together in
one call.
The core
method returns the underlying object. If
simplify=TRUE
, the default, then this is after
calling simplify2array
.
The notation reverses that of jQuery where $(obj).method is replaced in R with .(obj)$method. Okay, cute but is it of any use? Maybe not.
Returns an instance of a subclass of IterateR
.
1 2 3 4 5 6 7 8 9 10 11 | ## just like sapply(mtcars, median)
.(mtcars)$each(median) ## subclass of IterateR
## call core to get objects:
.(mtcars)$each(median)
## can chain calls if you want.
.(iris)$filter(is.numeric)$each(median)
## Sometimes we want both index and value (e.g., mapply(f, mtcars, seq_len(nrow(mtcars))))
.(mtcars)$each(function(x, i) sprintf('Column %s has max %s', i, max(x)))
## lists have a pluck method
.(mtcars)$each(function(x) c(mean=mean(x), sd=sd(x))) ## a table, but really stored as a list
.(mtcars)$each(function(x) c(mean=mean(x), sd=sd(x)))$pluck("mean") ## just mean values
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.