Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7500959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:22:16+00:00 2026-05-29T20:22:16+00:00

I have two data frames, Data1 and Data2, that I want to merge based

  • 0

I have two data frames, Data1 and Data2, that I want to merge based on a the variable “ID”.

This example data may be downloaded here: http://dl.dropbox.com/u/52600559/example.RData

Here is the first data frame:

> Data1
   ID     Fruit  Color Weight
1   1     Apple    Red      5
2   2    Orange Orange      7
3   3    Banana Yellow      3
4   4      Pear  Green      5
5   5    Tomato    Red      4
6   6     Berry   Blue      4
7   7  Mandarin Orange      4
8   8 Pineapple Yellow      9
9   9 Nectarine Orange      5
10 10      Beet    Red      5

And here is the second data frame:

> Data2
   ID       Fruit  Color Weight
1   1       Apple    Red      5
2   2      Orange Orange      7
3   3      Banana Yellow      3
4   4        Pear  Green      5
5   5      Tomato    Red      4
6  11 Pomegranate    Red      6
7  12       Grape  Green      4
8  13   Cranberry    Red      4
9  14       Melon   Pink      5
10 15     Pumpkin Orange     10

I have tried to merge them like this:

> merge(Data1, Data2, by = "ID", sort = FALSE, all.x = TRUE, all.y = TRUE)
   ID   Fruit.x Color.x Weight.x     Fruit.y Color.y Weight.y
1   1     Apple     Red        5       Apple     Red        5
2   2    Orange  Orange        7      Orange  Orange        7
3   3    Banana  Yellow        3      Banana  Yellow        3
4   4      Pear   Green        5        Pear   Green        5
5   5    Tomato     Red        4      Tomato     Red        4
6   9 Nectarine  Orange        5        <NA>    <NA>       NA
7   6     Berry    Blue        4        <NA>    <NA>       NA
8   7  Mandarin  Orange        4        <NA>    <NA>       NA
9   8 Pineapple  Yellow        9        <NA>    <NA>       NA
10 10      Beet     Red        5        <NA>    <NA>       NA
11 14      <NA>    <NA>       NA       Melon    Pink        5
12 11      <NA>    <NA>       NA Pomegranate     Red        6
13 12      <NA>    <NA>       NA       Grape   Green        4
14 13      <NA>    <NA>       NA   Cranberry     Red        4
15 15      <NA>    <NA>       NA     Pumpkin  Orange       10

As you can see, both data frames have many of the same variables. However, some IDs in Data1 are not in Data2, and vice versa. Moreover, some IDs are located in both data frames.

Question 1: I want to merge all of the columns that are shown above as well. So, I want “Fruit.x” to be merged with “Fruit.y”. into one column called “Fruit”. How can I do this?

Question 2: What if, for one of the samples that happens to be present in both Data1 and Data2, one of the values does not agree. So for sample ID 1, if Fruit.x is Apple, but Fruit.y is incorrectly coded as Aple (with a misspelling), is there a way I can check all of these instances quickly so that I can select which one is correct? Or can I tell R to always consider Data1 to be correct versus Data2 when this happens?

Thanks to anyone who can help!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:22:17+00:00Added an answer on May 29, 2026 at 8:22 pm

    Try this:

    merge(Data1, Data2, all = TRUE)
    

    and for spellings try this where amatch are the approximate matches to fruit and near contains the approximate matches that do not match exactly:

    for(fruit in Data1$Fruit) {
        amatch <- agrep(fruit, Data2$Fruit, value = TRUE)
        near <- amatch[amatch != fruit]
        if (length(near) > 0) cat(fruit, ":", near, "\n")
    }
    

    Using the data provided this gives:

    Berry : Cranberry 
    

    EDIT: improved clarity of code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two sheets having data like this sheet1 : **A** **B** **C** 752
I have two arrays of data that I'm trying to amalgamate. One contains actual
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two results from a data set. I want to add both results
I have the following matching problem: I have two data.frames, one with an observation
I have an application based on CoreDataBooks. I want to toggle two images in
I have two data frames DF1, DF2 each with 2 vectors DF1$A DF1$B, DF2$C,
I have two data frames A and B , both of the same dimensions.
I have two data frames like so: A B 1 6 2 7 5
I have two data frames. One contains a large amount of data. The second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.