hash (version 3.0.1)

hash: hash/associative array/dictionary data structure for the R language

Description

Functions for creating and working with hash objects:

Returns the number of items in a hash

Usage

hash(...)

is.hash(x)

## S3 method for class 'hash': as.list(x, all.names = FALSE, ...)

## S3 method for class 'hash': length(x)

Arguments

Value

hash hash object

is.hash logical value indicating if the argument is a hash.

as.list list conversion from hash

length integer; number of key-value pairs in the hash

integer Number of items in the hash.

docType

methods

Details

hash Class constructor

is.hash test if object is of class "hash"

as.list as.list.hash convert a hash object to a list

KEYS must be a valid R name, must be a character vector and must not be the empty string, "". When supplied by the used methods will try to coerce the keys to valid names using make.keys

VALUES are restricted to any valid R objects. HASH VALUES can be any R value, vector or object.

code{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. See .set for further details and how key-value vectors of unequal length are interpretted.

ACCESSORS. Hashes may be accessed via the standard R accessors [, [[ and $. See Extract for details.

PASS-BY REFERENCE. Environments and hashes are special objects in R because only one copy exists globally. When provided as an argument to a function, no local copy is made. When passes to functions, those functions can change the value of the hash. This is not typical of R.

PERFORMANCE. Hashes are based on R's native environments and are designed to be exceedingly fast using the environments internal hash table. For small data structures, a list will out-perform a hash in nearly every case. For larger data structure, i.e. > 500 key value pair the performance of the hash becomes faster. Much beyond that the performance of the hash far outperforms native lists.

Return the number of items in the hash by calling length on the internal environment.

See Also

Extract

See Also hash, length

Examples

Run this code
hash()      # Empty
  h <- hash() # give a name to the hash

  hash( key=letters, values=1:26 )  # Two lists/vectors or equal length

  hash( 1:3, lapply(1:3, seq, 1 ))

  hash( a=1, b=2, c=3 )             # key-value pairs
  hash( c(a=1, b=2, c=3) )          # vector of key-value pairs
  hash( list(a=1,b=2,c=3) )         # list of key-value pairs

  hash( c("foo","bar","baz"), 1:3 )
  hash( c("foo","bar","baz"),  lapply(1:3, seq, 1 ) )

  is.hash( hash() )
  as.list(h)   # CONVERT TO LIST
h <- hash( letters, 1:26 )
  length(h) # 26

Run the code above in your browser using DataLab