Kevin Wamae
2017-Oct-13 19:09 UTC
[R] Populate one data frame with values from another dataframe for rows that match
I'm trying to populate the column ?pf_mcl? in myDF1 with values from myDF2, where rows match based on column "studyno" but the solutions I have found so far don't seem to be giving me the desired output. Below is a snapshot of the data.frames. myDF1 <- structure(list(studyno = c("J1000/9", "J1000/9", "J1000/9", "J1000/9", "J1000/9", "J1000/9"), date = structure(c(17123, 17127, 17135, 17144, 17148, 17155), class = "Date"), pf_mcl = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_ ), year = c(2016, 2016, 2016, 2016, 2016, 2016)), .Names = c("studyno", "date", "pf_mcl", "year"), row.names = c(NA, 6L), class = "data.frame") myDF2 <- structure(list(studyno = c("J740/4", "J1000/9", "J895/7", "J931/6", "J609/1", "J941/3"), pf_mcl = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("studyno", "pf_mcl"), row.names = c(NA, 6L), class = "data.frame") myDF2 is a well curated subset of myDF1. Some rows in the two datasets match based on "studyno", one may find that values are missing in myDF1$pf_mcl or the values are wrong. All I want to do is identify a matching row in myDF2 and populate myDF1$pf_mcl with the value in myDF2$pf_mcl. If a row does not match based on ?studyno?, the value should remain the same. It's probably worth mentioning, the two data frames have other columns...I have selected a few for example purposes. ______________________________________________________________________ This e-mail contains information which is confidential. It is intended only for the use of the named recipient. If you have received this e-mail in error, please let us know by replying to the sender, and immediately delete it from your system. Please note, that in these circumstances, the use, disclosure, distribution or copying of this information is strictly prohibited. KEMRI-Wellcome Trust Programme cannot accept any responsibility for the accuracy or completeness of this message as it has been transmitted over a public network. Although the Programme has taken reasonable precautions to ensure no viruses are present in emails, it cannot accept responsibility for any loss or damage arising from the use of the email or attachments. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of KEMRI-Wellcome Trust Programme. ______________________________________________________________________ [[alternative HTML version deleted]]
Bert Gunter
2017-Oct-13 19:43 UTC
[R] Populate one data frame with values from another dataframe for rows that match
?merge Bert On Oct 13, 2017 12:09 PM, "Kevin Wamae" <KWamae at kemri-wellcome.org> wrote:> I'm trying to populate the column ?pf_mcl? in myDF1 with values from > myDF2, where rows match based on column "studyno" but the solutions I have > found so far don't seem to be giving me the desired output. > > Below is a snapshot of the data.frames. > > myDF1 <- structure(list(studyno = c("J1000/9", "J1000/9", "J1000/9", > "J1000/9", > "J1000/9", "J1000/9"), date = structure(c(17123, 17127, 17135, > 17144, 17148, 17155), class = "Date"), pf_mcl = c(NA_integer_, > NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_ > ), year = c(2016, 2016, 2016, 2016, 2016, 2016)), .Names = c("studyno", > "date", "pf_mcl", "year"), row.names = c(NA, 6L), class = "data.frame") > > myDF2 <- structure(list(studyno = c("J740/4", "J1000/9", "J895/7", > "J931/6", > "J609/1", "J941/3"), pf_mcl = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names > c("studyno", > "pf_mcl"), row.names = c(NA, 6L), class = "data.frame") > > myDF2 is a well curated subset of myDF1. Some rows in the two datasets > match based on "studyno", one may find that values are missing in > myDF1$pf_mcl or the values are wrong. > > All I want to do is identify a matching row in myDF2 and populate > myDF1$pf_mcl with the value in myDF2$pf_mcl. If a row does not match based > on ?studyno?, the value should remain the same. > > It's probably worth mentioning, the two data frames have other columns...I > have selected a few for example purposes. > > > > ______________________________________________________________________ > > This e-mail contains information which is confidential. It is intended > only for the use of the named recipient. If you have received this e-mail > in error, please let us know by replying to the sender, and immediately > delete it from your system. Please note, that in these circumstances, the > use, disclosure, distribution or copying of this information is strictly > prohibited. KEMRI-Wellcome Trust Programme cannot accept any responsibility > for the accuracy or completeness of this message as it has been > transmitted over a public network. Although the Programme has taken > reasonable precautions to ensure no viruses are present in emails, it > cannot accept responsibility for any loss or damage arising from the use of > the email or attachments. Any views expressed in this message are those of > the individual sender, except where the sender specifically states them to > be the views of KEMRI-Wellcome Trust Programme. > ______________________________________________________________________ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.[[alternative HTML version deleted]]
Rui Barradas
2017-Oct-13 20:34 UTC
[R] Populate one data frame with values from another dataframe for rows that match
Hello, Try the following. myDF1$studyno <- as.character(myDF1$studyno) myDF2$studyno <- as.character(myDF2$studyno) i1 <- which(names(myDF1) == "pf_mcl") merge(myDF1[-i1], myDF2, by = "studyno") Hope this helps, Rui Barradas Em 13-10-2017 20:09, Kevin Wamae escreveu:> I'm trying to populate the column ?pf_mcl? in myDF1 with values from myDF2, where rows match based on column "studyno" but the solutions I have found so far don't seem to be giving me the desired output. > > Below is a snapshot of the data.frames. > > myDF1 <- structure(list(studyno = c("J1000/9", "J1000/9", "J1000/9", "J1000/9", > "J1000/9", "J1000/9"), date = structure(c(17123, 17127, 17135, > 17144, 17148, 17155), class = "Date"), pf_mcl = c(NA_integer_, > NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_ > ), year = c(2016, 2016, 2016, 2016, 2016, 2016)), .Names = c("studyno", > "date", "pf_mcl", "year"), row.names = c(NA, 6L), class = "data.frame") > > myDF2 <- structure(list(studyno = c("J740/4", "J1000/9", "J895/7", "J931/6", > "J609/1", "J941/3"), pf_mcl = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("studyno", > "pf_mcl"), row.names = c(NA, 6L), class = "data.frame") > > myDF2 is a well curated subset of myDF1. Some rows in the two datasets match based on "studyno", one may find that values are missing in myDF1$pf_mcl or the values are wrong. > > All I want to do is identify a matching row in myDF2 and populate myDF1$pf_mcl with the value in myDF2$pf_mcl. If a row does not match based on ?studyno?, the value should remain the same. > > It's probably worth mentioning, the two data frames have other columns...I have selected a few for example purposes. > > > > ______________________________________________________________________ > > This e-mail contains information which is confidential. It is intended only for the use of the named recipient. If you have received this e-mail in error, please let us know by replying to the sender, and immediately delete it from your system. Please note, that in these circumstances, the use, disclosure, distribution or copying of this information is strictly prohibited. KEMRI-Wellcome Trust Programme cannot accept any responsibility for the accuracy or completeness of this message as it has been transmitted over a public network. Although the Programme has taken reasonable precautions to ensure no viruses are present in emails, it cannot accept responsibility for any loss or damage arising from the use of the email or attachments. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of KEMRI-Wellcome Trust Programme. > ______________________________________________________________________ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
Kevin Wamae
2017-Oct-14 05:48 UTC
[R] Populate one data frame with values from another dataframe for rows that match
Dear @Bert Gunter<mailto:bgunter.4567 at gmail.com>, I tried merge and I faced many challenges. @Rui Barradas<mailto:ruipbarradas at sapo.pt> solution is working. From: Bert Gunter <bgunter.4567 at gmail.com> Date: Friday, 13 October 2017 at 22:44 To: Kevin Wamae <KWamae at kemri-wellcome.org> Cc: R-help <R-help at r-project.org> Subject: Re: [R] Populate one data frame with values from another dataframe for rows that match ?merge Bert On Oct 13, 2017 12:09 PM, "Kevin Wamae" <KWamae at kemri-wellcome.org<mailto:KWamae at kemri-wellcome.org>> wrote: I'm trying to populate the column ?pf_mcl? in myDF1 with values from myDF2, where rows match based on column "studyno" but the solutions I have found so far don't seem to be giving me the desired output. Below is a snapshot of the data.frames. myDF1 <- structure(list(studyno = c("J1000/9", "J1000/9", "J1000/9", "J1000/9", "J1000/9", "J1000/9"), date = structure(c(17123, 17127, 17135, 17144, 17148, 17155), class = "Date"), pf_mcl = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_ ), year = c(2016, 2016, 2016, 2016, 2016, 2016)), .Names = c("studyno", "date", "pf_mcl", "year"), row.names = c(NA, 6L), class = "data.frame") myDF2 <- structure(list(studyno = c("J740/4", "J1000/9", "J895/7", "J931/6", "J609/1", "J941/3"), pf_mcl = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("studyno", "pf_mcl"), row.names = c(NA, 6L), class = "data.frame") myDF2 is a well curated subset of myDF1. Some rows in the two datasets match based on "studyno", one may find that values are missing in myDF1$pf_mcl or the values are wrong. All I want to do is identify a matching row in myDF2 and populate myDF1$pf_mcl with the value in myDF2$pf_mcl. If a row does not match based on ?studyno?, the value should remain the same. It's probably worth mentioning, the two data frames have other columns...I have selected a few for example purposes. ______________________________________________________________________ This e-mail contains information which is confidential. It is intended only for the use of the named recipient. If you have received this e-mail in error, please let us know by replying to the sender, and immediately delete it from your system. Please note, that in these circumstances, the use, disclosure, distribution or copying of this information is strictly prohibited. KEMRI-Wellcome Trust Programme cannot accept any responsibility for the accuracy or completeness of this message as it has been transmitted over a public network. Although the Programme has taken reasonable precautions to ensure no viruses are present in emails, it cannot accept responsibility for any loss or damage arising from the use of the email or attachments. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of KEMRI-Wellcome Trust Programme. ______________________________________________________________________ [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see 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 e-mail contains information which is confidential. It is intended only for the use of the named recipient. If you have received this e-mail in error, please let us know by replying to the sender, and immediately delete it from your system. Please note, that in these circumstances, the use, disclosure, distribution or copying of this information is strictly prohibited. KEMRI-Wellcome Trust Programme cannot accept any responsibility for the accuracy or completeness of this message as it has been transmitted over a public network. Although the Programme has taken reasonable precautions to ensure no viruses are present in emails, it cannot accept responsibility for any loss or damage arising from the use of the email or attachments. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of KEMRI-Wellcome Trust Programme. ______________________________________________________________________ [[alternative HTML version deleted]]
Kevin Wamae
2017-Oct-14 05:49 UTC
[R] Populate one data frame with values from another dataframe for rows that match
Dear @Rui Barradas, thank you for the solution. It works perfectly. On 13/10/2017, 23:35, "Rui Barradas" <ruipbarradas at sapo.pt> wrote: Hello, Try the following. myDF1$studyno <- as.character(myDF1$studyno) myDF2$studyno <- as.character(myDF2$studyno) i1 <- which(names(myDF1) == "pf_mcl") merge(myDF1[-i1], myDF2, by = "studyno") Hope this helps, Rui Barradas Em 13-10-2017 20:09, Kevin Wamae escreveu: > I'm trying to populate the column ?pf_mcl? in myDF1 with values from myDF2, where rows match based on column "studyno" but the solutions I have found so far don't seem to be giving me the desired output. > > Below is a snapshot of the data.frames. > > myDF1 <- structure(list(studyno = c("J1000/9", "J1000/9", "J1000/9", "J1000/9", > "J1000/9", "J1000/9"), date = structure(c(17123, 17127, 17135, > 17144, 17148, 17155), class = "Date"), pf_mcl = c(NA_integer_, > NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_ > ), year = c(2016, 2016, 2016, 2016, 2016, 2016)), .Names = c("studyno", > "date", "pf_mcl", "year"), row.names = c(NA, 6L), class = "data.frame") > > myDF2 <- structure(list(studyno = c("J740/4", "J1000/9", "J895/7", "J931/6", > "J609/1", "J941/3"), pf_mcl = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("studyno", > "pf_mcl"), row.names = c(NA, 6L), class = "data.frame") > > myDF2 is a well curated subset of myDF1. Some rows in the two datasets match based on "studyno", one may find that values are missing in myDF1$pf_mcl or the values are wrong. > > All I want to do is identify a matching row in myDF2 and populate myDF1$pf_mcl with the value in myDF2$pf_mcl. If a row does not match based on ?studyno?, the value should remain the same. > > It's probably worth mentioning, the two data frames have other columns...I have selected a few for example purposes. > > > > ______________________________________________________________________ > > This e-mail contains information which is confidential. It is intended only for the use of the named recipient. If you have received this e-mail in error, please let us know by replying to the sender, and immediately delete it from your system. Please note, that in these circumstances, the use, disclosure, distribution or copying of this information is strictly prohibited. KEMRI-Wellcome Trust Programme cannot accept any responsibility for the accuracy or completeness of this message as it has been transmitted over a public network. Although the Programme has taken reasonable precautions to ensure no viruses are present in emails, it cannot accept responsibility for any loss or damage arising from the use of the email or attachments. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of KEMRI-Wellcome Trust Programme. > ______________________________________________________________________ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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 e-mail contains information which is confidential. It is intended only for the use of the named recipient. If you have received this e-mail in error, please let us know by replying to the sender, and immediately delete it from your system. Please note, that in these circumstances, the use, disclosure, distribution or copying of this information is strictly prohibited. KEMRI-Wellcome Trust Programme cannot accept any responsibility for the accuracy or completeness of this message as it has been transmitted over a public network. Although the Programme has taken reasonable precautions to ensure no viruses are present in emails, it cannot accept responsibility for any loss or damage arising from the use of the email or attachments. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of KEMRI-Wellcome Trust Programme. ______________________________________________________________________
Possibly Parallel Threads
- Populate one data frame with values from another dataframe for rows that match
- Populate one data frame with values from another dataframe for rows that match
- Populate one data frame with values from another dataframe for rows that match
- Populate one data frame with values from another dataframe for rows that match
- Populate one data frame with values from another dataframe for rows that match