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"



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)



[1] "egg"

[1] "bacon"

[1] "ham"

[1] "bacon"



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


tapply (vals, keys, c)

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



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)[!]),"NA")[1])




All Articles