1

I have 5 temporal matrices. Each has a different number of rows and columns, but each has a first column called Year. Each start and end in a different year, so there is no common starting or ending date among all the matrices.

I want to combine all of the matrices into one based on the year those data were collected (i.e., the data collected in one matrix in a particular year, correspond to the data in other matrices for that same year).

For those matrices that do not have a corresponding year, I want to fill those blanks with NA. The data will not line up according to year just by using

Reduce(function(x,y) merge(x,y,1,all=T),list(matrix1, matrix2,...)) 

How can I line the data up and combine them into one matrix?

Matrix1:

Year    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  
1969    0   5   5   3   3   0   3   1   1   0   2   0   0   1   1   0   0   
1970    5   10  5   4   8   5   2   2   2   1   0   2   0   0   1   0   0   
1971    0   10  3   5   2   1   2   1   1   0   0   0   0   0   0   0   0   
1972    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   
...  
2014

Matrix2:

Year Checked  
 1969    1995  
 1970    2017  
 1971    1996  
 1972    1972  
 1973    2010  
 1974    2008  
...    
2015

Matrix3:

Year    WOak  
1983    1.43  
1984    1.08  
1985    2.01  
1986    1.32  
1987    1.16  
1988    3.16  
...  
2016

Matrix 4:

Year    C_Registered    M_Registered    P_Registered    T_Registered  
1969    NA  NA  NA  55  
1970    NA  NA  NA  72  
1971    NA  NA  NA  44  
1972    NA  NA  NA  0  
1973    NA  NA  NA  48  
1974    NA  NA  NA  59  
1975    NA  NA  NA  2  
...  
2015

Matrix5:

 Year Hunters  Trips Harvest TripsPerHunter  
 1949      NA     NA     873             NA  
 1950      NA     NA      NA             NA  
 1951      NA  11298     927             NA  
 1952      NA  19620    1047             NA  
 1953      NA  18040    1100             NA  
 1954      NA  15073    1200             NA  
 1955      NA  33373    4458             NA  
...  
2015
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Joe F
  • 21
  • 2
  • I think it will work if you add `by='Year'` to merge, `merge(x,y,1,all=T, by='Year')`. – Mosquite Dec 12 '17 at 21:01
  • Suggested duplicate: [Simultaneously merge multiple data frames in a list](https://stackoverflow.com/q/8091303/903061). – Gregor Thomas Dec 12 '17 at 21:03
  • If the `Reduce` solution doesn't work, you probably need to post a little bit of sample data. [Have a look at reproducibility tips here](https://stackoverflow.com/q/5963269/903061), but try to share maybe 3 small example data frames, 5-10 rows each should be plenty, and do it in a copy/pasteable way. `dput()` can help with that, or code to create the sample data. – Gregor Thomas Dec 12 '17 at 21:07
  • @Rich Scriven, MLavoie, KeLiuyue, Fruchtzwerg, KDeogharkar. I found the answer. When writing the answer, the question became "on hold". I consider both the question and my reply important. So, I kindly request the removal of "on hold" situation. – Erdogan CEVHER Dec 13 '17 at 07:03
  • I made edits to my question to include example data. I hope the question is no longer on hold. – Joe F Dec 13 '17 at 15:11
  • I've nominated the question for re-opening. You will probably have an easier time finishing the re-open process if you format the code in your question nicely (just highlight and press the code `{}` button in the editor). It would also be good to see the desired output corresponding to your sample input. – Gregor Thomas Dec 13 '17 at 17:09
  • I was able to get the Reduce() command to work using including by.x="Year". Thank you – Joe F Dec 13 '17 at 20:15

0 Answers0