extract-hstore: Extract or replace hstore values by key

Description Usage Arguments Details Value See Also Examples

Description

Operator to get or set values corresponding to a given key for all records in a hstore.

Usage

1
2
3
hstore %->% key

hstore %->% key <- value

Arguments

hstore

A hstore (i.e. list of lists).

key

Character string corresponding to a key in hstore.

value

Vector of values of the same length as hstore.

Details

Based on the hstore "->" operator in PostgreSQL, the %->% operator returns values associated with a given key for all records in the hstore. The assignment version of the operator i.e. hstore %->% key <- value either creates a new key-value pair or, if they key exists, update the associated value. It can also delete a key by assigning its value to NULL.

Note that to subset the records in hstore to which the operator applies, you must use single brackets so that the result remains a list of lists. See below for usage examples.

Value

For the extract version, a vector of the same length as hstore, containing the value corresponding to key for each record (or NA if none). For the replace version, the modified hstore.

See Also

new_hstore to create a empty hstore.

Examples

1
2
3
4
5
6
7
8
9
contacts <- data.frame(name = c("Anne", "Bert", "Chris"))
contacts$phone <- new_hstore(3)
contacts$phone %->% "home" <- c("555-123-4567", "555-923-9134", "555-276-1123")
contacts$phone[2:3] %->% "home"

contacts$phone[2] %->% "home" <- NULL
contacts$phone %->% "home"
contacts$phone[2] %->% "cell" <- "555-889-9134"
contacts$phone %->% "cell"

postGIStools documentation built on Sept. 20, 2019, 5:04 p.m.