Reputation: 4692
I have a data.frame
which has only one unique non-NA
value in all columns but one, which only has 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 lookup 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 have my lookup table refer to 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
Upvotes: 3
Views: 2189
Reputation: 66819
Here is one way. The lookup is a vector:
keys <- sapply(data,function(x)if(is.factor(x))levels(x)else "NA")
vals <- names(data)
lookup <- setNames(vals,keys)
I've replace NA
with "NA"
since I couldn't figure out how to use the former.
The syntax lookup[["egg"]]
works, but also lookup["egg"]
. The reverse lookup is rlookup <- keys
, accessible the same way: rlookup["A"]
.
For keys with multiple values. If the keys may map to a vector of values, use
lookup <- tapply(vals,keys,c)
Try this out with keys <- sapply(data,function(x)if(is.factor(x))levels(x)else "bacon")
and vals
as above, for example (as in the OP's comment, below). Now the lookup is a list and so can only be accessed with double brackets: lookup[["bacon"]]
. The reverse lookup works as before.
For general column classes. If the columns of data
are not all factors, the if
/else
conditions will need to be modified or generalized. 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])
Upvotes: 5