Make lookup table from data.frame

I have data.frame

one that has only one unique value not NA

in all columns except one that only has one NA

.

data <- data.frame(A = c("egg", "egg"), B = c(NA, "bacon"), C = c("ham", "ham"), D = c(NA, NA))

      

How can I use it to create a matching table of the form below?

lookup <- make_lookup(key=unique_values(data), value=names(data))
lookup[["egg"]] # returns "A"
lookup[["bacon"]] # returns "B"
lookup[["ham"]] # returns "C"
lookup[["NA"]] # returns "D"

      


EDIT

Based on Frank's answer below, I'm trying to get my lookup table to reference multiple values.

keys <- lapply(data, function(x) if(is.factor(x)) levels(x) else "bacon")
vals <- names(data)

      

keys

$A
[1] "egg"

$B
[1] "bacon"

$C
[1] "ham"

$D
[1] "bacon"

      

Vals

[1] "A" "B" "C" "D"

      

tapply (vals, keys, c)

Error in tapply(vals, keys, c) : arguments must have same length

      

+3


source to share


1 answer


Here's one way. Search is a vector:

keys <- sapply(data,function(x)if(is.factor(x))levels(x)else "NA")
vals <- names(data)

lookup <- setNames(vals,keys)

      

I replaced NA

with "NA"

since I couldn't figure out how to use the first one.

The syntax lookup[["egg"]]

works, but also lookup["egg"]

. Reverse Search rlookup <- keys

is available in the same way: rlookup["A"]

.


For keys with multiple values. If keys can map to a vector of values, use



lookup <- tapply(vals,keys,c)

      

Try it with keys <- sapply(data,function(x)if(is.factor(x))levels(x)else "bacon")

and vals

as above, for example (as in OP's comment, below). Now the search - a list, so access to them is only possible with double brackets: lookup[["bacon"]]

. The reverse lookup still works.


For general column classes. If the columns data

are not all factors, the conditions if

/ else

will need to be modified or summarized. Here is a version of @akrun's generalized solution from the comments:

keys <- sapply(data,function(x)c(unique(as.character(x)[!is.na(x)]),"NA")[1])

      

+5


source







All Articles