This actually corresponds to my other question but things get more complicated. I have data.frame and vector:
df <- data.frame(key=c(0,3:6), value=c(0,52,26,12,1))
x <- c(3,4,3,3,5,5,6,6,6,6)
and need to obtain values from df based on x as keys:
[1] 52 26 52 52 12 12 1 1 1 1
Solution from previous answer can only give result with NO duplicates:
df[df$key %in% x,"value"]
[1] 52 26 12 1
Is there a way to solve this?
Also, I see hash() can do things like:
h <- hash( keys=letters, values=1:26 )
h$a # 1
h[ "a" ]
h[[ "a" ]]
z <- rep(letters[3:5],2)
h[z] # still with NO duplicates
<hash> containing 3 key-value pair(s).
c : 3
d : 4
e : 5
But seems like it cannot return a vector of values with something like:
h[[z]]
Error in h[[z]] : wrong arguments for subsetting an environment
Otherwise, it would be perfect so that we can get rid of data.frame by using some ‘real’ hash concept.
Thanks!
To answer your first question: Use
matchYou should also have a look at Named vectors