Hi Pedro, I believe that the following should do what you want:
----start---- # Copy over to fill the NAs # Gets all Q16.x values that are set to NA, and assigns the equivalent Q16.y values f$Q16.x[is.na(f$Q16.x)] <- f$Q16.y[is.na(f$Q16.x)] f$Q17.x[is.na(f$Q17.x)] <- f$Q17.y[is.na(f$Q17.x)] # Keep and rename the columns we want f <- f[,c("NIU", "Q16.x", "Q17.y")] names(f) <- c("NIU", "Q16", "Q17") ----end---- Let me know if it doesn't work as it should. -Aidan On Thu, Jun 24, 2010 at 3:17 PM, Pedro Emmanuel Alvarenga Americano do Brasil <emmanuel.bra...@gmail.com> wrote: > Hello R friends, > > I would like to know if anyone could give me a tip of a function to merge > two data frames but updating NA fields. Any script that I could work on > would be fine too. > > I have two datasets which each one is from different physicians evaluating > the same subjects. The two data frames have the same variables. I woud like > to merge them but the second data frame 'inputing' its data on the empty > filds of the first one. I tried the merge function, but it does not work as > I would like to. I found other merge functions and a combine (Hmisc) but I > coudnt get it done so far. I did it very long ago using Stata (whith a > command called joinby), but a I couldnt find any like that in R. An example > below... > > > >c1 > NIU Q16 Q17 > 1 SIM *SIM > *2 *NA* NAO > 3 NAO SIM > 4* NA NA* > 5 SIM NAO > > c2 > NIU Q16 Q17 > 1 SIM *NAO* > 2 *NAO* NAO > 3 NAO SIM > 4 * SIM SIM* > 5 NA NA > > f <- merge(c1,c2,by.x='NIU',by.x='NIU',all.x=T,all.y=F) > f # using base::merge would give something like this ... > > NIU Q16.x Q17.x Q16.y Q17.y > 1 SIM SIM SIM NAO > 2 NA NAO NAO NAO > 3 NAO SIM NAO SIM > 4 NA NA SIM SIM > 5 SIM NAO NA NA > > > f # but I would like something like this... > > NIU Q16 Q17 > 1 SIM *SIM > *2 *NAO* NAO > 3 NAO SIM > 4 SIM SIM > 5 SIM NAO > > Any tip? > > Kind regards, > > Abraço forte e que a força esteja com você, > > Dr. Pedro Emmanuel A. A. do Brasil > Instituto de Pesquisa ClÃnica Evandro Chagas > Fundação Oswaldo Cruz > Rio de Janeiro - Brasil > Av. Brasil 4365 > Tel 55 21 3865-9648 > email: pedro.bra...@ipec.fiocruz.br > email: emmanuel.bra...@gmail.com > > ---Apoio aos softwares livres > www.zotero.org - gerenciamento de referências bibliográficas. > www.broffice.org ou www.openoffice.org - textos, planilhas ou > apresentações. > www.epidata.dk - entrada de dados. > www.r-project.org - análise de dados. > www.ubuntu.com/ - sistema operacional > > [[alternative HTML version deleted]] > > > _______________________________________________ > R-sig-Epi@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-epi > > [[alternative HTML version deleted]]
_______________________________________________ R-sig-Epi@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-epi