r - Merge and replace values from overlapping matrices -


i have 2 overlapping matrices shared columns , rows:

m.1 = matrix(c(na,na,1,na,na,na,1,1,1,na,1,1,1,1,1,na,1,1,1,na,na,na,1,na,na), ncol=5) colnames(m.1)  <- c("-2","-1","0","1","2") rownames(m.1)  <- c("-2","-1","0","1","2")  ##    -2 -1 0  1  2 ## -2 na na 1 na na ## -1 na  1 1  1 na ##  0  1  1 1  1  1 ##  1 na  1 1  1 na ##  2 na na 1 na na  m.2 = matrix(c(na,2,na,2,2,2,na,2,na), ncol=3) colnames(m.2)  <- c("-1","0","1") rownames(m.2)  <- c("-1","0","1")  ##    -1 0  1 ## -1 na 2 na ##  0  2 2  2 ##  1 na 2 na 

now want pass maximum value in each column m.1 , m.2 new matrix m.max, should this:

##    -2 -1 0  1  2 ## -2 na na 1 na na ## -1 na  1 2  1 na ##  0  1  2 2  2  1 ##  1 na  1 2  1 na ##  2 na na 1 na na 

based on previous threads, have meddled merge(), replace() , match() cannot desired result @ all, e.g.

m.max<- merge(m.1,m.2, = "row.names", all=true, sort = true)  ##   row.names -2 -1.x 0.x 1.x  2 -1.y 0.y 1.y ## 1        -1 na    1   1   1 na   na   2  na ## 2        -2 na   na   1  na na   na  na  na ## 3         0  1    1   1   1  1    2   2   2 ## 4         1 na    1   1   1 na   na   2  na ## 5         2 na   na   1  na na   na  na  na 

please help! on wrong track? operation require different kind of object matrix? example, tried convert matrices raster objects , cell statistics, ran problems because of unequal dimensions of m.1 , m.2.

importantly, answer should work larger objects, or whether want calculate maximum, minimum or sum.

you can use pmax:

#we create new matrix big m.1 values of m.2 in mres<-array(na,dim(m.1),dimnames(m.1)) mres[rownames(m.2),colnames(m.2)]<-m.2 #then use pmax pmax(m.1,mres,na.rm=true) #   -2 -1 0  1  2 #-2 na na 1 na na #-1 na  1 2  1 na #0   1  2 2  2  1 #1  na  1 2  1 na #2  na na 1 na na 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -