I am struggling to combine two matrices of unequal length, spanning across same (or similar) timeframe. I want to merge the information of the two matrices into one matrix according to the time dimension, filling zeroes on the rows where the information of the second matrix is missing.
In the following example, I have a 5x2 and 3x1 matrices with rownames equal to the corresponding time.
Input 1
[,1] [,2]
20160518 15:31:00 1 1
20160518 15:32:00 2 1
20160518 15:33:00 3 1
20160518 15:34:00 4 1
20160518 15:35:00 5 1
Input 2
[,1]
20160518 15:31:00 100
20160518 15:34:00 101
20160518 15:35:00 102
Desired result
[,1] [,2] [,3]
20160518 15:31:00 1 1 100
20160518 15:32:00 2 1 0
20160518 15:33:00 3 1 0
20160518 15:34:00 4 1 101
20160518 15:35:00 5 1 102
Second question would be very similar. Now instead of matching according to identical rownames, I am interested in matching according to identical values in a row of vector. I.e. imagine the rownames are a separate column of a given matrix (so I have a 5x3 and 3x2 matrices) and I want to combine them into one according to same logic as above.
I would really appreciate your help. I have searched for many hours to find the solution. I tried all sorts of merge, cbind and dplyr package commands. I am probably missing some small bit, but I cannot figure it out. The topics that came closest is (but I still cannot tailor it to my problem):
combining two data frames of different lengths
Best, P.