2

I have 5 common data frames that differ by year:

2010  
POLY STRA  Spp  breeders2010 nonbreeders2010   
1      1   mall     20             5  
1      1   cite     6              0  
1      3   mall     10             0   
2      1   gadw     6              2   

2011  
POLY STRA  Spp  breeders2011 nonbreeders2011   
1      1   mall     24             2  
1      1   cite     8              2  
1      3   mall     14             4   
2      1   gadw     8              4  

2012  
POLY STRA  Spp  breeders2012 nonbreeders2012   
1      1   mall     20             5  
1      1   cite     6              0  
1      3   mall     10             0   
2      1   gadw     6              2  

I need to merge these 5 data frames by POLY, STRA and Spp such that the final result looks like:

POLY STRA  Spp  breeders2010 nonbreeders2010 breeders2011 nonbreeders2011 breeders2012 nonbreeders2012 breeders2013 nonbreeders2013 breeders2014 nonbreeders2014     
1      1   mall     20            5               24            2             20             5 .....  
1      1   cite     6             0               8             2             6              0 ......  
1      3   mall     10            0               14            4             10             0 .....  
2      1   gadw     6             2               8             4             6              2 .....  

I have tried merge and cbind and I cannot get columns to line up properly.

1 Answer 1

4

I think merge should be just fine. For several data frames you can use Reduce:

Reduce(merge, list(df1, df2, df3, df4, df5))

If there are any missing rows in any of the data frames all = TRUE may help:

Reduce(function(x, y) merge(x, y, all = TRUE), list(df1, df2, df3, df4, df5))
Sign up to request clarification or add additional context in comments.

1 Comment

Can you help me with this question [stackoverflow.com/questions/35484595/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.