Search multiple fields in two data Frames in R -


i have 2 data frames:dfa , dfb.

data table: dfa                                     dfb fields:     name   job        check                 name   job             alice  analist                          alice  attendant             bob    attendant                        carl   professor             carl   professor                        bob    analist             daril  analist                          daril  analist 

if 2 fields same in both data tables third field in dfa should receive 1 value:

if (dfa$name==dfb$name , dfa$job == dfb$job) { dfa$check <- 1 }   data table: dfa                                     dfb fields:     name   job        check                 name   job             alice  analist    0                     alice  attendant             bob    attendant  0                     carl   professor             carl   professor  1                     bob    analist             daril  analist    0                     daril  analist 

i can loop found technique inefficient.

for ( x in 1:nrow(dfa)) {     ( y in 1:nrow(dfb)){         if ((as.character(dfa[x,7]) == as.character(dfb[y,5])) &&             (as.character(dfa[x,5]) == as.character(dfb[y,3])) ){                dfa[x,22] <- 1         }     } } 

does know way make more elegant, faster ? appreciate contributions .

this should simple enough base r, here's possible data.table solution allows add 1 column during merge while both updating by reference , in efficient manner using binary join

library(data.table) setkey(setdt(dfa), name, job) dfa[dfb, check := 1l] dfa #     name       job check # 1: alice   analist    na # 2:   bob attendant    na # 3:  carl professor     1 # 4: daril   analist     1 

as per explanation. keying dfa data set columns want merge (in order binary join work). performing binary join inserting dfb ith argument of dfa , simultaneously updating check column in place only when able join using := operator.

see here , here additional information.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -