How to not use loops & IF-statements in R -


i have 2 dataframes in r, 1 big imcomplete (import) , want create smaller, complete subset of (export). every id in $unique_name column unique, , not appear twice. other columns might example body mass, other categories correspond unique id. i've made code, double-loop , if-statement , work, slow:

for (j in 1:length(export$unique_name)){   (i in 1:length(import$unique_name)){     if (tostring(export$unique_name[j]) == tostring(import$unique_name[i])){       export$body_mass[j] <- import$body_mass[i]     }   } } 

i'm not r know bad way it. tips on how can functions apply() or perhaps plyr package?

bjørn

there many functions this. check out..

library(compare) compare(df1,df2,allowall=true) 

or mentioned @a.webb merge pretty handy function.

merge(x = df1, y = df2, by.x = "unique_id",by.y = "unique_id", all.x = t, sort = f) 

if prefer sql style statements

library(sqldf) sqldf('select * df1 intersect select * df2') 

easy implement , avoid for , if conditions


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -