Description Usage Arguments Format Details Value Note Author(s) Examples
This is a "destructuring bind" for R. It can be used to unpack structured lists into different variables, or achieve the effect of multiple return values from a function.
1 |
... |
a list of assignments, key on left, target variable on
right. That is, |
N/A
Element to variable matching should match R's argument binding rules, with the modification that arguments to the right of the ... will be matched positionally to elements at the end of the unpacked sequence. Calls to bind() can be nested to unpack nested structures.
You may leave an argument blank as in bind[, skipKey=,
...=rest] <- seq
to skip an element. (Here the first element of
seq
and the one tagged "skipKey" are both skipped and the
rest are gathered in the output variable rest
.)
Note that the assigned-to variable is on the right side of
each =
in the argument list. This is admittedly awkward but
is the best way to remain consistent with R's argument-binding
semantics.
a "bind" object, since it is invoked via a subset on "bind".
This will incidentally create a local variable named "bind"
in your environment. On the other hand if you have an object
already named "bind" and not of class "bind" this method won't be
found, so it's merely annoying and not destructive. It's not
clear how to avoid this and still use an assignment operator to do
the binding. (I could write a simple function, but I strongly
prefer there to be a <-
anywhere that there is a
modification to the environment.)
Nonlocal assignments (<<-
) are not supported and will behave
as local assignments.
Peter Meilstrup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #match by position
bind[x, y] <- c("foo", "bar")
#match by name
bind[a=x, b=y] <- c(b="bar", a="foo")
# one often wants to unpack the first and/or last, and rest of a list.
bind[first, ...=rest, last] <- letters
record <- list("Marilyn", "Monroe", dob=list("June", 1, 1926),
profession="film star", "born Norma Jean Baker",
donotuse="garbage", "1947 California Artichoke Queen",
list("August", 5, 1962))
bind[first, last,
dob=bind[month, day, year],
donotuse=, ...=notes, death] <- record
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.