cloudscomputes
cloudscomputes

Reputation: 1484

R replace value in a column by a dictionary table without using merge or join

Suppose I have a dict table like:

id value
1 168833
2 367656
3 539218
4 892211
......(millions of lines)

and a original data frame like:

name code 
Abo   1
Cm3   2
LL2   6
JJ    15

how to replace the code column in the original table with dictionary table without using join or merge?

Upvotes: 1

Views: 955

Answers (1)

akrun
akrun

Reputation: 887048

We can use match from base R

df1$value[match(df2$code, df1$id)]

Or another option is hashmap

library(hashmap)
hp <- hashmap(df1$id, df1$value)
hp[[df2$code]]

Based on the example in ?hashmap, it works faster

microbenchmark::microbenchmark(
     "R" = y[match(z, x)],
     "H" = H[[z]],
     times = 500L
 )
#Unit: microseconds
# expr     min       lq     mean   median       uq      max neval
#    R 154.197 202.1625 240.5838 229.1625 245.1735 6853.756   500
#    H  15.861  19.0235  22.7721  22.4490  24.9670   62.230   500

Upvotes: 8

Related Questions