Description Usage Arguments Value Author(s) References Examples
Operator acting on a list with named elements to extract list elements of interest to named variables.
1 2 3 4 5 6 |
x,y |
variable names. List elements at corresponding index are extracted and then assigned to those variables. |
... |
spaces for positions of list elements without interest. By default
these elements are not extracted to corresponding variables with same names.
Setting |
value |
a list object with named elements. |
list |
an exported structure variable with class attribute of |
all |
whether extract all named elements to variables. The default is FALSE except for two cases: no or all variables specified, in which all elements are extracted to corresponding variables. |
drop |
delete the dimensions of extracted variables which have only one level. The default is TRUE. |
named variables such as x
, y
and variables with element names.
Jun Cai (cai-j12@mails.tsinghua.edu.cn), Ph.D. student from Center for Earth System Science, Tsinghua University
http://stackoverflow.com/questions/1826519/function-returning-more-than-one-value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # unpack results from function returning multiple values
# define a function returning a matrix and a numeric, and the matrix can be
# dropped into a numeric
fun <- function() list(a = matrix(1, nr = 1, nc = 1), b = 2)
# implicitly extract all variabes without renaming. note that by default
# dimensions are dropped.
list[] <- fun() # return a = 1, b = 2
# explicitly extract all variables without renaming and dimension dropping
list[, , all = TRUE, drop = FALSE] <- fun() # return matrix a, b = 2
# only extract the first variable and assign it to x. note that by default only
# element of interest is extracted and renamed.
list[x, ] <- fun() # return x = 1
# set drop to FALSE without dimension dropping
list[x, , drop = FALSE] <- fun() # return matrix x
# set all to TRUE to extract all elements.
list[x, , all = TRUE] <- fun() # return x = 1, b = 2
# only extract the second variable and assign it to y
list[, y] <- fun() # return y = 2
# explicitly extract all variables with renaming
list[x, y] <- fun() # return x = 1, y = 2
# parameter all ignored
list[, all = FALSE] <- fun() # return a = 1, b = 2
list[x, y, all = FALSE] <- fun() # return x = 1, y = 2
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.