subset-.result: Extract variables from named elements of a list object

Description Usage Arguments Value Author(s) References Examples

Description

Operator acting on a list with named elements to extract list elements of interest to named variables.

Usage

1
2
3
4
5
6
S3 method for class 'result'
list[] <- value
list[x, ] <- value
list[, y] <- value
list[x, y, ..., all = FALSE] <- value
list[x, y, ..., drop = TRUE] <- value

Arguments

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 all to TRUE can also extract those elements without interest.

value

a list object with named elements.

list

an exported structure variable with class attribute of result.

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.

Value

named variables such as x, y and variables with element names.

Author(s)

Jun Cai (cai-j12@mails.tsinghua.edu.cn), Ph.D. student from Center for Earth System Science, Tsinghua University

References

http://stackoverflow.com/questions/1826519/function-returning-more-than-one-value

Examples

 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

caijun/TTmisc documentation built on May 13, 2019, 11 a.m.