Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

Combining two dataframes, similar rows, similar columns except one colum has different values

$
0
0

I have two dataframes that I want to combine into one. Rows in one dataframe are not always in the other and vice versa. I want to keep all rows and columns from both datasets. Additionally, there is one column ("Tag") that sometimes has different values for the same row of interest ("ID"). My dataset is too big to find them manually. I am having difficulty creating a separate column for when the values in "Tag" are not identical. I've tried test.output1 <-union(test.df1, test.df2,suffix=c(".1",".2")) but get an error due to some of the identical columns having different factor levels. I've also tried variations of: test.output2<-rbind.fill(test.df1, test.df2) but end up with duplicate rows.

Any help is greatly appreciated. Thanks!

Example:

#dataframe 1
test.df1
ID     Year   Location Tag    Length
H1     2013   Site1    272   46
H2     2013   Site2    236   984
H3     2014   Site3    150   68
H4     2014   Site4    698   12
H34    2015   Site1    594   65

#dataframe 3
test.df2
ID     Year   Species Tag    
H1     2013   1       631 
H2     2013   2       236
H3     2014   3       755
H4     2014   4       698
H12    2017   3       135

#What I would like the output to be
test.df.3 
ID     Year   Location  Species Tag.1   Tag.2  Length
H1     2013   Site1     1       272     631    46
H2     2013   Site2     2       236     NA     984
H3     2014   Site3     3       150     755    68
H4     2014   Site4     4       698     NA     12
H12    2017   NA        3       135     NA     20
H34    2015   Site1     NA      594     NA     65


Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>