Merging two data frames according to row values

Traviskorte

I have two data frames, each with the same two columns: county codes and frequencies. They aren't identical, but some of the county code values show up in both data frames. Like this:

"county_code","freq"
"01011",2
"01051",1
"01073",9
"01077",1

"county_code","freq"
"01011",4
"01056",2
"01073",1
"01088",6

I want to merge them into a new data frame, such that if a county code appears in both data frames, their respective frequencies are added together. If the county code just appears in one or the other of the data frames, I want to add it (and its frequency) to the new data frame unchanged. The result should look like this:

"county_code","freq"
"01011",6
"01051",1
"01056",2
"01073",10
"01077",1
"01088",6

The result doesn't have to be ordered. I tried to use reshape for this, but I wasn't sure that was the right approach. Thoughts?

Matthew Lundberg

Combine the two data frames with rbind, then use aggregate to collapse multiple rows with the same county_code:

aggregate(freq~county_code, rbind(d1, d2) , FUN=sum)
##   county_code freq
## 1        1011    6
## 2        1051    1
## 3        1073   10
## 4        1077    1
## 5        1056    2
## 6        1088    6

(Using the definitions in MrFlick's answer.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Merging two data frames based on row values in python pandas

From Dev

Merging two data frames Pandas

From Dev

Merging data frames of different row length in R

From Dev

Merging two data frames and doing calculation

From Dev

Merging two data frames based on the index column

From Dev

Merging two data.frames by key column

From Dev

Merging two data.frames with overlapping intervals

From Dev

Merging two data frames on a common column in python

From Dev

lapply() and spline() on two data frames in R , No Merging

From Dev

merging 2 data frames and matching their values in pandas

From Dev

R - Merging Two Data.Frames with Row-Level Conditional Variables

From Dev

Merging two pandas data frames in python using outer merge not identifying identical values

From Dev

Python Pandas merging two data frames and map one row from one data frame to all rows from the other

From Dev

Select only the first row when merging data frames with multiple matches

From Dev

Merging data frames row-wise and column-wise in R

From Dev

Merging data frames in a list

From Dev

In R, if site and date match in two data frames, pull row values from first data frame

From Dev

Merging two data frames with different sizes by matching their columns

From Dev

Efficiently merging two data frames on a non-trivial criteria

From Dev

Merging two data frames, both with coordinates based on the closest location

From Dev

Merging 2 data frames without changing associated values

From Dev

matching two data frames and change values in one of the data frames

From Java

In R, how to combine two data frames where column names in one equals row values in another?

From Dev

Replace values between two data frames in R

From Dev

Comparing the difference in values between two data frames

From Dev

Merge two data frames on multiple values

From Dev

Match columns of two data frames according to a reference column that is common on both data frames in R

From Dev

Merging data frames stored in lists

From Dev

merging list of multiple data frames

Related Related

  1. 1

    Merging two data frames based on row values in python pandas

  2. 2

    Merging two data frames Pandas

  3. 3

    Merging data frames of different row length in R

  4. 4

    Merging two data frames and doing calculation

  5. 5

    Merging two data frames based on the index column

  6. 6

    Merging two data.frames by key column

  7. 7

    Merging two data.frames with overlapping intervals

  8. 8

    Merging two data frames on a common column in python

  9. 9

    lapply() and spline() on two data frames in R , No Merging

  10. 10

    merging 2 data frames and matching their values in pandas

  11. 11

    R - Merging Two Data.Frames with Row-Level Conditional Variables

  12. 12

    Merging two pandas data frames in python using outer merge not identifying identical values

  13. 13

    Python Pandas merging two data frames and map one row from one data frame to all rows from the other

  14. 14

    Select only the first row when merging data frames with multiple matches

  15. 15

    Merging data frames row-wise and column-wise in R

  16. 16

    Merging data frames in a list

  17. 17

    In R, if site and date match in two data frames, pull row values from first data frame

  18. 18

    Merging two data frames with different sizes by matching their columns

  19. 19

    Efficiently merging two data frames on a non-trivial criteria

  20. 20

    Merging two data frames, both with coordinates based on the closest location

  21. 21

    Merging 2 data frames without changing associated values

  22. 22

    matching two data frames and change values in one of the data frames

  23. 23

    In R, how to combine two data frames where column names in one equals row values in another?

  24. 24

    Replace values between two data frames in R

  25. 25

    Comparing the difference in values between two data frames

  26. 26

    Merge two data frames on multiple values

  27. 27

    Match columns of two data frames according to a reference column that is common on both data frames in R

  28. 28

    Merging data frames stored in lists

  29. 29

    merging list of multiple data frames

HotTag

Archive