I am trying to merge two data frames using two common columns using the following code.
data = merge(df1, df2,by.x=c("b_id"), by.y=c("e_id"), all=T)
This works fine. BUT there are some rows (cases of data) which have an ID and data for the second data frame, and not the first (and vice versa). This means I return lines of NA for the first data frame (or vice versa).
I am wondering how I could return a merged data set where the second data frames ID number is appended to the first data frames ID number in the merged data frame. In programmes like SPSS or STATA it does this automatically if you merge two data sets with differing completeness of data.
e.g. I want to return this.
b_id dfv1 dfv2
1101 5 NA
1102 5 5
1103 8 9
1104 NA 3
1105 NA 12
Not this!
b_id dfv1 dfv2
1101 5 NA
1102 5 5
1103 8 9
NA NA 3
NA NA 12
From these two dataframes:
b_id dfv1
1101 5
1102 5
1103 8
NA NA
NA NA
e_id dfv2
NA NA
1102 5
1103 9
1104 3
1105 12
Thanks