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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:30:35+00:00 2026-05-28T01:30:35+00:00

I would like to merge two data frames, but do not want to duplicate

  • 0

I would like to merge two data frames, but do not want to duplicate rows if there is more than one match. Instead I would like to sum the observations on that day.

From ?merge: The rows in the two data frames that match on the specified columns are extracted, and joined together. If there is more than one match, all possible matches contribute one row each.

Here’s some example code:

days <- as.data.frame(as.Date(c("2012-1-1", "2012-1-2", "2012-1-3", "2012-1-4")))

names(days) <- "Date"
obs.days <- as.data.frame(as.Date(c("2012-1-2", "2012-1-3", "2012-1-3")))
obs.days$count <- 1
colnames(obs.days) <- c("Date", "Count")
df <- merge(days, obs.days, by.x="Date", by.y="Date", all.x=TRUE)

I would like the final data frame to only list 2012-1-3 one time with a count value of 2.

  • 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-28T01:30:35+00:00Added an answer on May 28, 2026 at 1:30 am

    I’d suggest you merge them and then aggregate them (essentially perform a SUM for each unique Date).

    df <- merge(z.days,obs.days, by.x="Date", by.y="Date", all.x=TRUE)
            Date Count
    1 2012-01-01    NA
    2 2012-01-02     1
    3 2012-01-03     1
    4 2012-01-03     1
    5 2012-01-04    NA
    

    Now to do the merge you could use aggregate:

    df2 <- aggregate(df$Count,list(df$Date),sum)
         Group.1  x
    1 2012-01-01 NA
    2 2012-01-02  1
    3 2012-01-03  2
    4 2012-01-04 NA
    names(df2)<-names(df)
    

    BUT I’d recommend package plyr, which is awesome! In particular, function ddply.

    library(plyr)
    ddply(df,.(Date),function(x) data.frame(Date=x$Date[1],Count=sum(x$Count)))
            Date Count
    1 2012-01-01    NA
    2 2012-01-02     1
    3 2012-01-03     2
    4 2012-01-04    NA
    

    The command ddply(df,.(Date),FUN) essentially does:

    for each date in unique(df$Date):
        add to output dataframe FUN( df[df$Date==date,] )
    

    So the function I’ve provided creates a data frame of one row with columns Date and Count, being the sum of all counts for that date.

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

Sidebar

Related Questions

I would like to be able to merge two files into one during configure
I am using Oracle, I need to merge two queries, but i would like
I have two classes that I would like to merge into a composite. These
I have two unix partitions under debian which I would like to merge (disk
I have multiple datasets that I would like to combine into one. There is
I want to update rows of a table in a specific order, like one
I would like to know how to merge these two Javascript ajax scripts. So
Need help to perform merge two similar contact values. I would like to know
Is there a way to merge two exe files into one, programmatically, so that
I've got many, many mp3 files that I would like to merge into a

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.