lookup: Data Key Lookup

Description Usage Arguments Details Value See Also Examples

View source: R/lookup.R

Description

Find rows with specified keys.

Usage

1
2
3
lookup(x, table, default = NA)

rowid(table, x, default = NA)

Arguments

x

the values to be matched.

table

the values to be matched against.

default

the value to return when no row in the table matches.

Details

The lookup and rowid functions to the same thing, but have their arguments transposed. Each finds the corresponding entry in the table for each entry in x; for values in x with no match, return the default value.

The rowid function is generic. The default implementation converts table with as.keyset and converts x with as.dataset, then finds the rows in the table corresponding to the rows in x.

The lookup function calls the rowid generic.

Value

A vector of row indices in table for the corresponding rows in the x argument. Rows with no match get the default value (converted to numeric).

See Also

keyset, match.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# look up single-component keys
table <- keyset(name = rownames(mtcars))
lookup(c("Valiant", "Fiat 128", "Delorean"), table)

# rowid returns the same result but has its arguments swapped
rowid(table, c("Valiant", "Fiat 128", "Delorean"))

# multi-component keys
keys <- keyset(k1 = rep(LETTERS[1:4], each = 8), k2 = rep(1:8, 4))

# look up a single item
lookup(record(k1 = "A", k2 = 5), keys)

# look up multiple items
lookup(dataset(k1 = c("A", "A", "B", "Z"),
               k2 = c(  5, 100,   1,   3)), keys)

patperry/r-frame documentation built on May 6, 2019, 8:34 p.m.