Hello, I have two rows which are almost identical but miss different values at different locations. I would like to merge these two rows so that the missing values are replaced by the element in the same column on the other row making one row. If both rows contain a NA, NA remains in the output. 1 2 3 4 5 Row1 AA AG GG NA NA Row2 NA AG GG AA NA The output i want is one row 1 2 3 4 5 Row AA AG GG AA NA I tried to use the functions match,grep, and gsub but I can''t seem to make it work I would appreciate any suggestions! Best regards, Iris Kolder [[alternative HTML version deleted]]
Dimitris Rizopoulos
2007-Oct-11 13:51 UTC
[R] Matching and merging two rows with missing values
try something like this: rw1 <- c("AA", "AG", "GG", NA, NA) rw2 <- c(NA, "AG", "GG", "AA", NA) ####### rw <- rw1 rw[is.na(rw)] <- rw2[is.na(rw)] rw I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Iris Kolder" <I.C.Kolder at amc.uva.nl> To: <r-help at stat.math.ethz.ch> Sent: Thursday, October 11, 2007 3:32 PM Subject: [R] Matching and merging two rows with missing values> Hello, > > > > I have two rows which are almost identical but miss different values > at > different locations. I would like to merge these two rows so that > the > missing values are replaced by the element in the same column on the > other > row making one row. If both rows contain a NA, NA remains in the > output. > > > > 1 2 3 4 5 > > Row1 AA AG GG NA NA > > Row2 NA AG GG AA NA > > > > The output i want is one row > > > > 1 2 3 4 5 > > Row AA AG GG AA NA > > > > I tried to use the functions match,grep, and gsub but I can't seem > to make > it work I would appreciate any suggestions! > > > > Best regards, > > > > Iris Kolder > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Thank you for your help this works!! x <-c("AA","AG","GG",NA, NA) y <- c(NA, "AG", "GG", "AA", NA) x[is.na(x)] <- y[is.na(x)] Hello, I have two rows which are almost identical but miss different values at different locations. I would like to merge these two rows so that the missing values are replaced by the element in the same column on the other row making one row. If both rows contain a NA, NA remains in the output. 1 2 3 4 5 Row1 AA AG GG NA NA Row2 NA AG GG AA NA The output i want is one row 1 2 3 4 5 Row AA AG GG AA NA I tried to use the functions match,grep, and gsub but I can't seem to make it work I would appreciate any suggestions! Best regards, Iris Kolder [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -------------------------------------------------------- This is not an offer (or solicitation of an offer) to buy/sell the securities/instruments mentioned or an official confirmation. Morgan Stanley may deal as principal in or own or act as market maker for securities/instruments mentioned or may advise the issuers. This is not research and is not from MS Research but it may refer to a research analyst/research report. Unless indicated, these views are the author's and may differ from those of Morgan Stanley research or others in the Firm. We do not represent this is accurate or complete and we may not update this. Past performance is not indicative of future returns. For additional information, research reports and important disclosures, contact me or see https://secure.ms.com/servlet/cls. You should not use e-mail to request, authorize or effect the purchase or sale of any security or instrument, to send transfer instructions, or to effect any other transactions. We cannot guarantee that any such requests received via e-mail will be processed in a timely manner. This communication is solely for the addressee(s) and may contain confidential information. We do not waive confidentiality by mistransmission. Contact me if you do not wish to receive these communications. In the UK, this communication is directed in the UK to those persons who are market counterparties or intermediate customers (as defined in the UK Financial Services Authority's rules).