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 9268309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:46:41+00:00 2026-06-18T14:46:41+00:00

My problem is very similar to the one posted here . The difference is

  • 0

My problem is very similar to the one posted here.

The difference is that they knew the columns that would be conflicting whereas I need a generic method that wont know in advance which columns conflict.

example:

TABLE1
Date             Time    ColumnA    ColumnB
01/01/2013      08:00      10         30
01/01/2013      08:30      15         25
01/01/2013      09:00      20         20
02/01/2013      08:00      25         15
02/01/2013      08:30      30         10
02/01/2013      09:00      35         5

TABLE2
Date           ColumnA    ColumnB    ColumnC
01/01/2013      100        300         1
02/01/2013      200        400         2

Table 2 only has dates and so is applied to all fields in table A that match the date regardless on time.

I would like the merge to sum the conflicting columns into 1. The result should look like this:

TABLE3
Date             Time    ColumnA    ColumnB    ColumnC
01/01/2013      08:00      110         330        1
01/01/2013      08:30      115         325        1
01/01/2013      09:00      120         320        1
02/01/2013      08:00      225         415        2
02/01/2013      08:30      230         410        2
02/01/2013      09:00      235         405        2

At the moment my standard merge just creates duplicate columns of “ColumnA.x”, “ColumnA.y”, “ColumnB.x”, “ColumnB.y”.

Any help is much appreciated

  • 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-18T14:46:43+00:00Added an answer on June 18, 2026 at 2:46 pm

    If I understand correctly, you want a flexible method that does not require knowing which columns exist in each table aside from the columns you want to merge by and the columns you want to preserve. This may not be the most elegant solution, but here is an example function to suit your exact needs:

    merge_Sum <- function(.df1, .df2, .id_Columns, .match_Columns){
        merged_Columns <- unique(c(names(.df1),names(.df2)))
        merged_df1 <- data.frame(matrix(nrow=nrow(.df1), ncol=length(merged_Columns)))
        names(merged_df1) <- merged_Columns
        for (column in merged_Columns){
            if(column %in% .id_Columns | !column %in% names(.df2)){
                merged_df1[, column] <- .df1[, column]
            } else if (!column %in% names(.df1)){
                merged_df1[, column] <- .df2[match(.df1[, .match_Columns],.df2[, .match_Columns]), column]
            } else {
                df1_Values=.df1[, column]
                df2_Values=.df2[match(.df1[, .match_Columns],.df2[, .match_Columns]), column]
                df2_Values[is.na(df2_Values)] <- 0
                merged_df1[, column] <- df1_Values + df2_Values
            }
        }
        return(merged_df1)
    }
    

    This function assumes you have a table ‘.df1’ that is a master of sorts, and you want to merge data from a second table ‘.df2’ that has rows that match one or more of the rows in ‘.df1’. The columns to preserve from the master table ‘.df1’ are accepted as an array ‘.id_Columns’, and the columns that provide the match for merging the two tables are accepted as an array ‘.match_Columns’

    For your example, it would work like this:

    merge_Sum(table1, table2, c("Date","Time"), "Date")
    
    #   Date       Time  ColumnA ColumnB ColumnC
    # 1 01/01/2013 08:00     110     330       1
    # 2 01/01/2013 08:30     115     325       1
    # 3 01/01/2013 09:00     120     320       1
    # 4 02/01/2013 08:00     225     415       2
    # 5 02/01/2013 08:30     230     410       2
    # 6 02/01/2013 09:00     235     405       2
    

    In plain language, this function first finds the total number of unique columns and makes an empty data frame in the shape of the master table ‘.df1’ to later hold the merged data. Then, for the ‘.id_Columns’, the data is copied from ‘.df1’ into the new merged data frame. For the other columns, any data that exists in ‘.df1’ is added to any existing data in ‘.df2’, where the rows in ‘.df2’ are matched based on the ‘.match_Columns’

    There is probably some package out there that does something similar, but most of them require knowledge of all the existing columns and how to treat them. As I said before, this is not the most elegant solution, but it is flexible and accurate.

    Update: The original function assumed a many-to-one relationship between table1 and table2, and the OP requested the allowance of a many-to-none relationship, also. The code has been updated with a slightly less efficient but 100% more flexible logic.

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

Sidebar

Related Questions

This question is very similar to that posed here . My problem is that
My problem is very similar to the one linked below except that if there
I have a problem very similar to the one described here: https://microsoft.public.win32.programmer.kernel.narkive.com/Ly2P8Yp2/prevent-vista-from-marking-my-application-as-non-responding That thread
This problem is very similar to this one: Xcode 4 Preview 4 displays "Build
My current problem is very similar to this one . I have a downloadFile(URL)
I have a problem which seems very similar to the one described in http://markmail.org/message/6rlrzkgyx3pspmnf
I have a very similar problem to the one described at [JasperReport parameters works
I have a problem very similar to the one mentioned in this question .
I have a very similar problem to the one described in this question .
I have multiple html elements. They use very similar css styles. Only difference is

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.