Gregg Powell
2021-Jun-13 21:30 UTC
[R] Need to compare two columns in two data.frames and return all rows from df where rows values are missing
This is even complicated to write into a question.... Have two data.frames (A and B) data.frame A and B each have a name column. Want to compare A and B data.frame to each other based on the values in the 'names' columns - for every name that appears in dataframe A? but not B, I want to copy the corresponding rows to a third dataframe C, and for every name that appears in B but not A, I want to copy the corresponding rows to a fourth dataframe D. I can then row bind the dataframes C and D together and get a complete list of all the rows that were missing in either A or B. try as I might - I can't get this to work. Can someone help P-L-E-A-S-E!!!! Thanks, Gregg Powell AZ, USA -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 509 bytes Desc: OpenPGP digital signature URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210613/96ea49b3/attachment.sig>
Bert Gunter
2021-Jun-13 23:06 UTC
[R] Need to compare two columns in two data.frames and return all rows from df where rows values are missing
... but I *think* merge(A, B, by = "name", all = TRUE) is what you want. Rows of NA's correspond to rows that were in one but not the other. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Jun 13, 2021 at 2:38 PM Gregg Powell via R-help < r-help at r-project.org> wrote:> This is even complicated to write into a question.... > > Have two data.frames (A and B) > > data.frame A and B each have a name column. Want to compare A and B > data.frame to each other based on the values in the 'names' columns - for > every name that appears in dataframe A but not B, I want to copy the > corresponding rows to a third dataframe C, and for every name that appears > in B but not A, I want to copy the corresponding rows to a fourth dataframe > D. I can then row bind the dataframes C and D together and get a complete > list of all the rows that were missing in either A or B. > > try as I might - I can't get this to work. Can someone help P-L-E-A-S-E!!!! > > > Thanks, > Gregg Powell > AZ, USA______________________________________________ > 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]]
William Michels
2021-Jun-13 23:17 UTC
[R] Need to compare two columns in two data.frames and return all rows from df where rows values are missing
Maybe something like this?> df_A <- data.frame(names=LETTERS[1:10], values_A=1:10) > df_B <- data.frame(names=LETTERS[6:15], values_B=11:20) > df_AB <- merge(df_A, df_B, by="names") > df_AAB <- merge(df_A, df_AB, all.x=TRUE) > df_BAB <- merge(df_B, df_AB, all.x=TRUE) > df_C <- df_AAB[is.na(df_AAB$values_B), ] > df_D <- df_BAB[is.na(df_BAB$values_A), ] > df_ABnames values_A values_B 1 F 6 11 2 G 7 12 3 H 8 13 4 I 9 14 5 J 10 15> df_Cnames values_A values_B 1 A 1 NA 2 B 2 NA 3 C 3 NA 4 D 4 NA 5 E 5 NA> df_Dnames values_B values_A 6 K 16 NA 7 L 17 NA 8 M 18 NA 9 N 19 NA 10 O 20 NA>HTH, Bill. W. Michels, Ph.D. On Sun, Jun 13, 2021 at 2:38 PM Gregg Powell via R-help <r-help at r-project.org> wrote:> > This is even complicated to write into a question.... > > Have two data.frames (A and B) > > data.frame A and B each have a name column. Want to compare A and B data.frame to each other based on the values in the 'names' columns - for every name that appears in dataframe A but not B, I want to copy the corresponding rows to a third dataframe C, and for every name that appears in B but not A, I want to copy the corresponding rows to a fourth dataframe D. I can then row bind the dataframes C and D together and get a complete list of all the rows that were missing in either A or B. > > try as I might - I can't get this to work. Can someone help P-L-E-A-S-E!!!! > > > Thanks, > Gregg Powell > AZ, USA______________________________________________ > 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.