ListVector: Vector that can be subsetted as a list

Description Usage Arguments Value Author(s) See Also Examples

View source: R/ListVector.R

Description

Vector that can be subsetted as a list. This class has special subsetting methods, that is, "[", "[[", and "$" that allows the object to be treated as as a vector or as a list.

Usage

1
ListVector(..., .overrideNames=TRUE)

Arguments

...

Named arguments to be put in the vector. All elements from the same argument gets the same name in the vector. If one of the arguments is a list it is recursively unwrapped into a named vector.

.overrideNames

If TRUE, the names of named elements in a list element are overridden by the name of the list element itself.

Value

Return a named vector of class ListVector. If no arguments were given, NULL is returned.

Author(s)

Henrik Bengtsson

See Also

vector(). list().

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Creating a ListVector
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
theta <- ListVector(a=1:5, b=2, c=100:103, bb=22:24)
print(theta)

# If arguments contains lists these are unwrapped into
# named vectors.
theta2 <- ListVector(list(list(a=1:5), b=2), c=100:103, bb=22:24)
stopifnot(identical(theta, theta2))

# Unnamed elements in a list get the same name as the list
theta3 <- ListVector(list(a=list(1:5), b=2), c=100:103, bb=22:24)
stopifnot(identical(theta, theta3))

# Creating a ListVector out of another ListVector work too.
theta5 <- ListVector(theta)
stopifnot(identical(theta, theta5))

# Creating a ListVector out of another vector as well.
theta4 <- ListVector(as.vector(theta))
stopifnot(identical(theta, theta4))

# Overriding element names
xi <- ListVector(c=list(a=2, b=3))
print(xi)  # Note that 'a' and 'b' are ignored since 'c' is given.



# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Subsetting a ListVector with "$" and "[[" like a list, but with
# partial name matching.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

cat("--------------------------------------------------\n")
cat("List-style subsetting\n")
cat("--------------------------------------------------\n")
cat("Partial name matching:\n")
print(theta$a)
print(theta$b)
print(theta$c)

print(theta[["a"]])
print(theta[["b"]])
print(theta[["c"]])

cat("Recursive name matching:\n")
print(theta[[c("a", "c")]])

cat("Non-partial name matching:\n")
print(theta[["a", .partial=FALSE]])
print(theta[["b", .partial=FALSE]])
print(theta[["c", .partial=FALSE]])

cat("Non-existing elements:\n")
print(theta[["na"]])

cat("as.list():\n")
print(as.list(theta))


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Subsetting a ListVector with "[" like a vector, but with
# partial name matching.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cat("--------------------------------------------------\n")
cat("Vector-style subsetting\n")
cat("--------------------------------------------------\n")
cat("Partial name matching:\n")
print(theta["a"])
print(theta["b"])
print(theta["c"])

cat("Non-partial name matching:\n")
print(theta["a", .partial=FALSE])
print(theta["b", .partial=FALSE])
print(theta["c", .partial=FALSE])

cat("as.vector():\n")
print(as.vector(theta))

HenrikBengtsson/R.basic documentation built on May 6, 2019, 11:51 p.m.