Convenience Functions For Lists

Share:

Description

Convenience structures for creating, manipulating, and reporting on lists.

Usage

1
2
3
4
5
6
7
8
listGrid(x, KEEP.OUT.ATTRS=TRUE, stringsAsFactors=FALSE)

listMean(x, w=NULL)
listSum(x)

namedDataFrame(..., stringsAsFactors=FALSE) 
namedList(...)
namedVector(...)

Arguments

x

for listGrid, a list of which each element is a vector; for listMean and listSum, a list of numeric scalars, vectors, matrices, or data frames all having the same dimension.

KEEP.OUT.ATTRS, stringsAsFactors

as the equivalent arguments to expand.grid.

w

optional vector of weights.

...

any number of objects.

Details

listGrid is a replacement for expand.grid in which the last factor, rather than the first as in expand.grid, varies fastest.

listMean and listSum return the (optionally weighted) mean and sum, respectively, of a list of numeric elements. All elements of x must have the same dimension.

The named* functions return named objects of the appropriate type containing the objects in the ... argument. For example, instead of writing x <- list(y=y, z=z), one may write x <- namedList(x, y).

Value

For listGrid, a data frame. For listMean and listSum, an object of the same dimension as the elements of x. For named*, an object of the appropriate class.

Author(s)

Daniel Dvorkin

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
species <- c("cat", "dog")
status <- c("spayed", "neutered", "intact")
age <- c(2, 5, 7)
listGrid(namedList(species, status, age))
#    species   status age
# 1      cat   spayed   2
# 2      cat   spayed   5
# 3      cat   spayed   7
# 4      cat neutered   2
# 5      cat neutered   5
# 6      cat neutered   7
# 7      cat   intact   2
# 8      cat   intact   5
# 9      cat   intact   7
# 10     dog   spayed   2
# 11     dog   spayed   5
# 12     dog   spayed   7
# 13     dog neutered   2
# 14     dog neutered   5
# 15     dog neutered   7
# 16     dog   intact   2
# 17     dog   intact   5
# 18     dog   intact   7

set.seed(123)
x <- replicate(3, matrix(rnorm(6), ncol=3), simplify=FALSE)
x
# [[1]]
#            [,1]       [,2]      [,3]
# [1,] -0.5604756 1.55870831 0.1292877
# [2,] -0.2301775 0.07050839 1.7150650
# 
# [[2]]
#            [,1]       [,2]      [,3]
# [1,]  0.4609162 -0.6868529 1.2240818
# [2,] -1.2650612 -0.4456620 0.3598138
# 
# [[3]]
#           [,1]       [,2]       [,3]
# [1,] 0.4007715 -0.5558411  0.4978505
# [2,] 0.1106827  1.7869131 -1.9666172
listSum(x)
#           [,1]      [,2]      [,3]
# [1,]  0.301212 0.3160143 1.8512200
# [2,] -1.384556 1.4117596 0.1082617
listMean(x, 1:3)
#            [,1]       [,2]       [,3]
# [1,]  0.2606119 -0.2470868  0.6785005
# [2,] -0.4047086  0.7566540 -0.5775265

namedList(species, status, age)
# $species
# [1] "cat" "dog"
# 
# $status
# [1] "spayed"   "neutered" "intact"  
# 
# $age
# [1] 2 5 7

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.