hash/associative array/dictionary data structure for the R language
hash.RdPreferred constructor for the hash-class.
Usage
hash(...)
is.hash(x)
# S3 method for class 'hash'
as.list(x, all.names = FALSE, ... )Details
hash returns a hash object. Key-value pairs may be specified
via the ... argument as explicity arguments keys and
values, as named key-value pairs, as a named vector or as implicit
key, value vectors. See examples below for each type.
Keys must be a valid R name, must be a character vector and must not be
the empty string, "". Values are restricted to any valid R objects.
See .set for further details and how key-value vectors of
unequal length are interpretted.
Hashes may be accessed via the standard R accessors [, [[
and \$. See hash-accessors for details.
is.hash returns a boolean value indicating if the argument is
a hash object.
as.list.hash coerces the hash to a list.
Examples
hash()
#> <hash> containing 0 key-value pair(s).
#> NA : NULL
hash( key=letters, values=1:26 )
#> <hash> containing 2 key-value pair(s).
#> key : a b c d e f g h i j k l m n o p q r s t u v w x y z
#> values : 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
hash( 1:3, lapply(1:3, seq, 1 ))
#> <hash> containing 3 key-value pair(s).
#> 1 : 1
#> 2 : 2 1
#> 3 : 3 2 1
hash( a=1, b=2, c=3 )
#> <hash> containing 3 key-value pair(s).
#> a : 1
#> b : 2
#> c : 3
hash( c(a=1, b=2, c=3) )
#> <hash> containing 3 key-value pair(s).
#> a : 1
#> b : 2
#> c : 3
hash( list(a=1,b=2,c=3) )
#> <hash> containing 3 key-value pair(s).
#> a : 1
#> b : 2
#> c : 3
hash( c("foo","bar","baz"), 1:3 )
#> <hash> containing 3 key-value pair(s).
#> bar : 2
#> baz : 3
#> foo : 1
hash( c("foo","bar","baz"), lapply(1:3, seq, 1 ) )
#> <hash> containing 3 key-value pair(s).
#> bar : 2 1
#> baz : 3 2 1
#> foo : 1
hash( letters, 1:26 )
#> <hash> containing 26 key-value pair(s).
#> a : 1
#> b : 2
#> c : 3
#> d : 4
#> e : 5
#> f : 6
#> g : 7
#> h : 8
#> i : 9
#> j : 10
#> k : 11
#> l : 12
#> m : 13
#> n : 14
#> o : 15
#> p : 16
#> q : 17
#> r : 18
#> s : 19
#> t : 20
#> u : 21
#> v : 22
#> w : 23
#> x : 24
#> y : 25
#> z : 26
h <- hash( letters, 1:26 )
h$a
#> [1] 1
h$b
#> [1] 2
h[[ "a" ]]
#> [1] 1
h[ letters[1:3] ]
#> <hash> containing 3 key-value pair(s).
#> a : 1
#> b : 2
#> c : 3
h$a<-100
# h[['a']]<-letters
is.hash(h)
#> [1] TRUE
as.list(h)
#> $i
#> [1] 9
#>
#> $j
#> [1] 10
#>
#> $k
#> [1] 11
#>
#> $l
#> [1] 12
#>
#> $m
#> [1] 13
#>
#> $n
#> [1] 14
#>
#> $o
#> [1] 15
#>
#> $p
#> [1] 16
#>
#> $q
#> [1] 17
#>
#> $r
#> [1] 18
#>
#> $s
#> [1] 19
#>
#> $t
#> [1] 20
#>
#> $u
#> [1] 21
#>
#> $v
#> [1] 22
#>
#> $w
#> [1] 23
#>
#> $x
#> [1] 24
#>
#> $y
#> [1] 25
#>
#> $z
#> [1] 26
#>
#> $a
#> [1] 100
#>
#> $b
#> [1] 2
#>
#> $c
#> [1] 3
#>
#> $d
#> [1] 4
#>
#> $e
#> [1] 5
#>
#> $f
#> [1] 6
#>
#> $g
#> [1] 7
#>
#> $h
#> [1] 8
#>
clear(h)
rm(h)