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

  • SEARCH
  • Home
  • 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 9224075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:13:37+00:00 2026-06-18T04:13:37+00:00

I am trying to merge two data of different size by ID. However, for

  • 0

I am trying to merge two data of different size by ID. However, for the values that match, both data contain duplicated entries, i.e., there may be three ID #3 in Data A and three ID#3 in Data B. When I try to merge the data, the result is much larger than both data combined.

C<-merge(A,B,by="ID",all.x=T,sort=F)

I want to merge the two data by the ID column, such that the first ID #3 in B pairs with the first ID #3 in A, and so on.

Also, I want the row order of Data A to remain the same. The sort=FALSE wasn’t much helpful: It places all the matching rows at the top, and the unmatched rows at the bottom.

Thanks for your 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-06-18T04:13:39+00:00Added an answer on June 18, 2026 at 4:13 am

    Before merging, you’ll need to add to each data.frame a column whose value records the index of each observation within its own ID group.

    ## Example data
    A <- data.frame(ID=c(1,1,1,2), ht=1:4)
    B <- data.frame(ID=c(1,1,2,2), wt=3:6)
    
    ## Add column with number of each observation within ID
    A <- transform(A, ID2=ave(ID, ID, FUN=seq_along))
    B <- transform(B, ID2=ave(ID, ID, FUN=seq_along))
    
    ## Now carry out the merge
    merge(A, B, all.x=TRUE, sort=FALSE)
    #   ID ID2 ht wt
    # 1  1   1  1  3
    # 2  1   2  2  4
    # 3  2   1  4  5
    # 4  1   3  3 NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to merge two arrays for the values of array1 that are set
I'm trying to merge two file that have the same structure, and some data
I am trying to merge two sets of analytical data from two different providers.
I am trying to merge two data.frames together, based on a common column name
I am trying to merge the data from two tables into one result but
im trying to merge two arrays together. both have numeric keys and are unique.
Hi im trying to merge two tables. I have ids that exists only in
I'm trying to merge two branches together using mercurial and there are some conflicts.
I have a set of structured data, that I'm trying to merge together, based
I am trying to merge two repositories, and there are only a couple of

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.