An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111003/5cb090c0/attachment.pl>
Merge two data frames and find common values and non-matching values
5 messages · francy, William Dunlap, Sarah Goslee +1 more
Start out with merge():
> df <- merge(df1, df2, all.x=TRUE) # could add by="location" for emphasis
> df
location Name Position Country
1 36 cristina B <NA>
2 75 francesca A UK
You could make make your 'Match' column from is.na(df$Country) if you
knew that df2$Country were never NA. Otherwise you can add a fake
variable to the merge to tell which output rows come from unmatched
rows in the first data.frame:
> df12 <- merge(df1, cbind(df2, fromDF2=TRUE), all.x=TRUE, by="location")
> df12$Match <- !is.na(df12$fromDF2)
> df12
location Name Position Country fromDF2 Match
1 36 cristina B <NA> NA FALSE
2 75 francesca A UK TRUE TRUE
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of francy Sent: Monday, October 03, 2011 10:55 AM To: r-help at r-project.org Subject: [R] Merge two data frames and find common values and non-matching values Hi, I am trying to find a function to match two data frames of different lengths for one field only. So, for example, df1 is: Name Position location francesca A 75 cristina B 36 And df2 is: location Country 75 UK 56 Austria And I would like to match on "Location" and the output to be something like: Name Position Location Match francesca A 75 1 cristina B 36 0 I have tried with the function 'match' or with: subset(df1, location %in% df2) But it does not work. Could you please help me figure out how to do this? Thank you! -f -- View this message in context: http://r.789695.n4.nabble.com/Merge-two-data-frames-and-find-common- values-and-non-matching-values-tp3868299p3868299.html Sent from the R help mailing list archive at Nabble.com. [[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.
Hi,
On Mon, Oct 3, 2011 at 1:54 PM, francy <francy.casalino at gmail.com> wrote:
Hi, I am trying to find a function to match two data frames of different lengths for one field only. So, for example, df1 is: Name Position location francesca A 75 cristina B 36 And df2 is: location Country 75 UK 56 Austria And I would like to match on "Location" and the output to be something like:
Sounds like you need merge() (just as in your subject line!).
Name Position Location Match francesca A 75 1 cristina B 36 0 I have tried with the function 'match' or with: subset(df1, location %in% df2) But it does not work. Could you please help me figure out how to do this?
Sarah
Sarah Goslee http://www.functionaldiversity.org
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111004/eb6f6385/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111004/2b21942e/attachment.pl>