-1

I have two data frames with some overlapping variables and some not. Each variable has an attribute (frequency of variable) and I need to combine the two into one dataframe where the result is two columns of attributes, one corresponding to the first dataframe, and the second corresponding to the first data frame, and the union of all the variables are represented.

dataframe 1:

var frequency
a   3
b   2
d   5

dataframe 2:

var frequency
a   2
b   3
c   3

Resulting dataframe:

var frequency1 frequency2
a   3          2
b   2          3
c   0          3
d   5          0

Thanks for your help.

xjtc55
  • 389
  • 2
  • 4
  • 14

1 Answers1

1

This seems to work for me:

df1 = read.csv('df1.csv')
df2 = read.csv('df2.csv')
df1$frequency1 = df1$frequency
df2$frequency2 = df2$frequency
df1$frequency = NULL
df2$frequency = NULL

df = merge(df1, df2, by = 'var', all = TRUE)
print(df)

The idea is that if you want frequency1 and frequency2 to be the names in the final merged dataframe, you can rename them in df1 and df2 before merging. This produces:

  var frequency1 frequency2
1   a          3          2
2   b          2          3
3   d          5         NA
4   c         NA          3
James Thompson
  • 46,512
  • 18
  • 65
  • 82