Match and replace operation in data frame in R -


let's dataset following:

john   na    kaira    carry  john  na maya   sam   maya leo    paty  leo tinker na    tinker fabo   leo   maya 

i have dataset:

john   1 carry  2 maya   3 leo    4 tinker 5 fabo   6 sam    7 paty   8  kaira  9 

i want match values of above table (df2) first table (df1) final table (df) looks following:

1   na   9    2   1    na 3   7    3 4   8    4 5   na   5 6   4    3 

could do

df1[] <- match(unlist(df1), df2$v1) #   v1 v2 v3 # 1  1 na  9 # 2  2  1 na # 3  3 na  3 # 4  4  8  4 # 5  5 na  5 # 6  6  4  3 

if numbers in df2 not in order, abit adjust code be

df1[] <- df2[match(unlist(df1), df2$v1), 2] 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -