Replicating rows based on id in R
I have this dataframe ( mydf1
) where the serial number (in sequential column) is repeated. I want to replicate rows in another dataframe ( mydf2
) based on the number of samples the serial appears in mydf1 and get a table result
. Thanks for the help!
mydf1
serial var1 var2
122 a d
222 b e
321 c f
321 fd fs
222 bx eg
mydf2
serial varA varB
122 an ddf
222 cb edf
321 ff ffg
result
serial varA varB
122 an ddf
222 cb edf
222 cb edf
321 ff ffg
321 ff ffg
+3
source to share
2 answers
If I am confused using string indexing:
`rownames<-`(mydf2,mydf2$serial)[sort(as.character(mydf1$serial)),]
# serial varA varB
#122 122 an ddf
#222 222 cb edf
#222.1 222 cb edf
#321 321 ff ffg
#321.1 321 ff ffg
The same result in 2 steps:
rownames(mydf2) <- mydf2$serial
mydf2[sort(as.character(mydf1$serial)),]
If you want to avoid any values NA
for non-compliant cases in mydf1
, then change the middle part of the call to:
as.character(mydf1$serial %in% mydf2$serial)
+4
source to share