How can I merge multiple matrices of different dimensions in R -
this question has answer here:
i have these matrices of different dimensions. key.related.sheet
column in matrices have common , uniques values. want match common rows , merge 3 matrices, want include unique rows well. result column should have key.related.sheet
, sample_b
, trace_1
,trace_2
, trace_3
columns only. can please me this?
aa<-structure(c("s05-f13-p01:s05-f13-p01", "s05-f13-p01:s08-f10-p01", "s05-f13-p01:s08-f11-p01", "s05-f13-p01:s09-f66-p01", "s05-f13-p01", "s08-f10-p01", "s08-f11-p01", "s09-f66-p01", "1.25", "0.227", "-0.183", "-0.217"), .dim = c(4l, 3l), .dimnames = list(null, c("key.related.sheet", "sample_b", "trace_1"))) bb<-structure(c("s05-f13-p01:s08-f10-p01", "s05-f13-p01:s08-f11-p01", "s05-f13-p01:s09-f66-p01", "s05-f13-p01:s09-f67-p01", "s08-f10-p01", "s08-f11-p01", "s09-f66-p01", "s09-f67-p01", "0.227", "-0.183", "-0.217", "0.292", "unknown", "unknown", "unknown", "unknown" ), .dim = c(4l, 4l), .dimnames = list(null, c("key.related.sheet", "sample_b", "trace_2", "type"))) cc<-structure(c("s05-f13-p01:s08-f11-p01", "s05-f13-p01:s09-f66-p01", "s05-f13-p01:s09-f67-p01", "s05-f13-p01:s09-f68-p01", "s05-f13-p01:s09-f01-p01", "s08-f11-p01", "s09-f66-p01", "s09-f67-p01", "s09-f68-p01", "s09-f01-p01", "-0.183", "-0.217", "0.292", "-0.314", "0.0418"), .dim = c(5l, 3l), .dimnames = list(null, c("key.related.sheet", "sample_b", "trace_3")))
the expected output be:
key.related.sheet sample_b trace_1 trace_2 trace_3 "s05-f13-p01:s05-f13-p01" "s05-f13-p01" "1.25" "s05-f13-p01:s08-f10-p01" "s08-f10-p01" "0.227" "0.227" "s05-f13-p01:s08-f11-p01" "s08-f11-p01" "-0.183" "-0.183" "-0.183" "s05-f13-p01:s09-f66-p01" "s09-f66-p01" "-0.217" "-0.217" "-0.217" "s05-f13-p01:s09-f67-p01" "s09-f67-p01" "0.292" "0.292" "s05-f13-p01:s09-f68-p01" "s09-f68-p01" "-0.314" "s05-f13-p01:s09-f01-p01" "s09-f01-p01" "0.0418"
two nested merges , removal of extraneous column
merge(merge(aa,bb[, -4], by=c("key.related.sheet", "sample_b") ,all=true), cc, by=c("key.related.sheet", "sample_b") ,all=true) key.related.sheet sample_b trace_1 trace_2 trace_3 1 s05-f13-p01:s05-f13-p01 s05-f13-p01 1.25 <na> <na> 2 s05-f13-p01:s08-f10-p01 s08-f10-p01 0.227 0.227 <na> 3 s05-f13-p01:s08-f11-p01 s08-f11-p01 -0.183 -0.183 -0.183 4 s05-f13-p01:s09-f66-p01 s09-f66-p01 -0.217 -0.217 -0.217 5 s05-f13-p01:s09-f67-p01 s09-f67-p01 <na> 0.292 0.292 6 s05-f13-p01:s09-f01-p01 s09-f01-p01 <na> <na> 0.0418 7 s05-f13-p01:s09-f68-p01 s09-f68-p01 <na> <na> -0.314
Comments
Post a Comment