Replicate rows based on id number in R -


i have dataframe (mydf1) serial number(in serial column) repeated. want replicate rows in dataframe (mydf2) based on number of counts serial appear in mydf1 , result table. thank help!

mydf1  serial  var1 var2     122        d        222     b    e        321     c    f      321    fd   fs    222    bx   eg    mydf2  serial  vara   varb     122       ddf        222    cb    edf        321    ff    ffg    result   serial  vara  varb      122      ddf         222    cb   edf      222    cb   edf         321    ff   ffg      321    ff   ffg 

if i'm being tricky, using row indexing:

`rownames<-`(mydf2,mydf2$serial)[sort(as.character(mydf1$serial)),] #      serial vara varb #122      122    ddf #222      222   cb  edf #222.1    222   cb  edf #321      321   ff  ffg #321.1    321   ff  ffg 

same result in 2 steps:

rownames(mydf2) <- mydf2$serial mydf2[sort(as.character(mydf1$serial)),] 

if want avoid having na values non-matching cases in mydf1, change middle part of call to:

as.character(mydf1$serial %in% mydf2$serial) 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -