Merge two data frames, but only include variables where there are no NAs

Vegard Dyran

I have two data frames that I want to merge:

df1:

Date         Company    Return
1988-09-30   BELSHIPS   0.087
1988-10-31   BELSHIPS   0.021
1988-11-30   BELSHIPS   0.015
1988-12-30   BELSHIPS   -0.048
1988-09-30   GOODTECH   0.114
1988-10-31   GOODTECH   0.074
1988-11-30   GOODTECH   NA
1988-12-30   GOODTECH   NA
1988-09-30   LABOREMUS  -0.014
1988-10-31   LABOREMUS  0.024
1988-11-30   LABOREMUS  0.017
1988-12-30   LABOREMUS  0.021

df2:

Company
BELSHIPS
BIK BOK
FARSTAD SHIPPING
GOODTECH
GYLDENDAL

I want to merge the two data frames by Company, but I only want to include companies that have no NAs in return. The new data frame should therefore look like this:

df3:

Date         Company    Return
1988-09-30   BELSHIPS   0.087
1988-10-31   BELSHIPS   0.021
1988-11-30   BELSHIPS   0.015
1988-12-30   BELSHIPS   -0.048

Only the company BELSHIPS is included, because GOODTECH has NAs in Return and LABOREMUS is not included in df2.

I have tried df3 <- merge(df2, df1[!is.na(df1$Return)], by = "Company") which doesn't work, because it only omits the rows with NAs, not the entire company.

Any suggestions as to how I can fix this?

pogibas

Base R solution:

# Select companies that have NA
# You can also use unique on this
foo <- df1$Company[is.na(df1$Return)]
# Subset data frame where Company is within df2 and doesn't have NA
subset(df1, Company %in% df2$Company & !Company %in% foo)

#         Date  Company Return
# 1 1988-09-30 BELSHIPS  0.087
# 2 1988-10-31 BELSHIPS  0.021
# 3 1988-11-30 BELSHIPS  0.015
# 4 1988-12-30 BELSHIPS -0.048

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Merge a single column in two data frames in R where only some rows match

From Dev

R: merge 2 data frames, one of them with repeated measures, keeping NAs where appropriate

From Dev

How to combine two data frames where only one column is same in two data frames in R

From Dev

Merge and concatenate two data frames in R

From Dev

Merge two data frames to fill in missing dates

From Dev

How to merge and sum two data frames

From Dev

Merge two data frames and select specific columns

From Dev

Merge two data.frames with replacement

From Dev

R: Merge two data frames by common columns

From Dev

How to merge and compute two data frames?

From Dev

Merge two data frames on multiple values

From Dev

How to merge to two pandas data frames?

From Dev

Can I replace NAs when joining two data frames with dplyr?

From Dev

R: merge two data frames when either of two criteria matches

From Dev

Pandas Data Frame - Merge Two Data Frames based on "InStr" > 0

From Dev

R Merge 2 data.frames with similar variables

From Dev

Merge two data frames with non-unique keys

From Java

pandas: merge (join) two data frames on multiple columns

From Dev

pyspark: merge (outer-join) two data frames

From Java

Merge two data frames by row and column names and by group

From Dev

How to merge two data frames based on nearest date

From Java

Merge two data frames while keeping the original row order

From Dev

Merge two R data frames and identify the source of each row

From Dev

R issues with merge/rbind/concatenate two data frames

From Dev

Python Pandas merge two data frames based on multiple values field

From Dev

How to merge two data.frames by the first part of a names in a column?

From Dev

Add (not merge!) two data frames with unequal rows and columns

From Dev

How to merge two data frames based on different column names

From Dev

how to merge two data frames based on particular column in pandas python?

Related Related

  1. 1

    Merge a single column in two data frames in R where only some rows match

  2. 2

    R: merge 2 data frames, one of them with repeated measures, keeping NAs where appropriate

  3. 3

    How to combine two data frames where only one column is same in two data frames in R

  4. 4

    Merge and concatenate two data frames in R

  5. 5

    Merge two data frames to fill in missing dates

  6. 6

    How to merge and sum two data frames

  7. 7

    Merge two data frames and select specific columns

  8. 8

    Merge two data.frames with replacement

  9. 9

    R: Merge two data frames by common columns

  10. 10

    How to merge and compute two data frames?

  11. 11

    Merge two data frames on multiple values

  12. 12

    How to merge to two pandas data frames?

  13. 13

    Can I replace NAs when joining two data frames with dplyr?

  14. 14

    R: merge two data frames when either of two criteria matches

  15. 15

    Pandas Data Frame - Merge Two Data Frames based on "InStr" > 0

  16. 16

    R Merge 2 data.frames with similar variables

  17. 17

    Merge two data frames with non-unique keys

  18. 18

    pandas: merge (join) two data frames on multiple columns

  19. 19

    pyspark: merge (outer-join) two data frames

  20. 20

    Merge two data frames by row and column names and by group

  21. 21

    How to merge two data frames based on nearest date

  22. 22

    Merge two data frames while keeping the original row order

  23. 23

    Merge two R data frames and identify the source of each row

  24. 24

    R issues with merge/rbind/concatenate two data frames

  25. 25

    Python Pandas merge two data frames based on multiple values field

  26. 26

    How to merge two data.frames by the first part of a names in a column?

  27. 27

    Add (not merge!) two data frames with unequal rows and columns

  28. 28

    How to merge two data frames based on different column names

  29. 29

    how to merge two data frames based on particular column in pandas python?

HotTag

Archive