R: Create, project or concatenate key-value pairs

keyval {rmr2} | R Documentation

## Create, project or concatenate key-value pairs

### Description

Create a key-value object (a collecton of key-value pairs) from two R objects, extract keys or values from a key value object or concatenate multiple key value objects

### Usage

      keys(kv)
      values(kv)
      keyval(key, val)
      c.keyval(...)

### Arguments

`kv` | key-value pairs
`key` | the desired key or keys
`val` | the desired value or values
`...` | key-value pairs to concatenate, or a single list thereof

### Details

The `keyval` function is used to create return values for the map and reduce functions, themselves parameters to `mapreduce`. Key-value objects are also appropriate arguments for the `to.dfs` function and are returned by `from.dfs`. `keys` and `values` extract keys and values resp. from a key value object. `c.keyval` concatenates two or more key-value objects by concatenating the keys and values separately after recycling the arguments. When invoked with a single argument, it considers it a list of key value objects to concatenate. A key value object should always be considered vectorized, meaning that it defines a collection of key-value pairs. For the purpose of forming key-value pairs, the length of an object is considered its number of rows whene defined, that is for matrices and data frames, or its R `length` otherwise. Consistently with this definition, the n-th element of a key or value argument is its n-th row or a subrange including only the n-th element otherwise. Data types are preserved, meaning that, for instance, if the `key` is a matrix its n-th element is a matrix with only one row, the n-th row of the larger matrix (the behavior of the `[]` operator with `drop = FALSE`). The same is true for data frames, list and atomic vectors. When `key` and `val` have different lengths according to this definition, recycling is applied. The pairing between keys and values is positional, meaning that the n-th element of the key argument is associated with the n-th element of the val argument in a single key-value pair. Concatenation happens with `rbind` or variants thereof whenever keys or values have rows, `c` otherwise. Mixing and matching keys of different type, e.g. a matrix with a vector, is not supported, and the same is true for keys, but key and value in the same keyval object do not need to be of the same type. When porting programs from rmr < 2 a list of non-vectorized key-value pairs can be converted with `c.keyval(keyval(list(k1), list(v1)), keyval(list(k2), list(v2)), ...)`. In many cases wrapping the keys and values in a `list` call is not necessary, but it is in the general case.

### Examples

    #single key-val
      keyval(1,2)
      keys(keyval(1,2))
      values(keyval(1,2))
    #10 kv pairs of the form (i,i)
      keyval(1:10, 1:10)
    #2 kv pairs (1, 2i-1) and  (2, 2i) for i in 1:5
      keyval(1:2, 1:10)
    # mtcars is a data frame, each row is a value with key set to the value of column cyl
      keyval(mtcars$cyl, mtcars)
    # concatenate two keyval objects
      c.keyval(keyval(1,1:2), keyval(1,1:3))

[Package rmr2 version 3.3.1 Index]