Hi Arun,

Yes, that last command m1[indx2N] <- m2[sort(indx1)] did exactly the trick,
now the variable and their values are perfectly matched. Thanks a lot for
your great help.

Best,

Elio


On Wed, Mar 5, 2014 at 1:17 AM, arun <smartpink111@yahoo.com> wrote:

>
>
> Hi Elio,
>
> If you change the last line of the code:
>
> m1[indx2N] <- m2[indx1]
>
> to
> m1[indx2N] <- m2[sort(indx1)]
>
>  sum(m1[rownames(m1)=="p79",])
> #[1] 191
>  sum(m2[rownames(m2)=="p79",])
> #[1] 191
>
>
> The problem is in the order of the colnames/rownames in each of the
> datasets.  I tested it on something like:
> m1 <- matrix(0,12,12,dimnames=rep(list(paste0("a",1:12)),2))
>  set.seed(49)
>  m2 <-
> matrix(sample(0:2,4*4,replace=TRUE),ncol=4,dimnames=rep(list(c("a4","a6","a9","a12")),2))
>
> which didn't show the problem.
>
>
> But, if I change the order of colnames:
>
> m1 <- matrix(0,12,12,dimnames=rep(list(paste0("a",c(1:3,8:12,4:7))),2))
>
> set.seed(49)
>  m2 <-
> matrix(sample(0:2,4*4,replace=TRUE),ncol=4,dimnames=rep(list(c("a9","a4","a6","a12")),2))
> vec1 <- paste0(rownames(m1)[row(m1)],colnames(m1)[col(m1)])
> vec2 <- paste0(rownames(m2)[row(m2)],colnames(m2)[col(m2)])
> indx <- match(vec1,vec2)
> indx1 <- indx[!is.na(indx)]
>
> indx2 <- match(vec2,vec1)
> indx2N <- indx2[!is.na(indx2)]
> m1[indx2N] <- m2[sort(indx1)]
> m1
>
>
> Hope this helps.
> A.K.
>
>
>
>
>
>
> On Tuesday, March 4, 2014 3:16 PM, Elio Shijaku <selius@gmail.com> wrote:
>
> Hi Arun,
>
> Sorry to disturb, but while trying your code to combine two matrices of
> unequal dimensions, I noticed a problem, the sum for each variable does not
> match, but the overall variables sum does match for both matrices,
> something isn't going right, perhaps the variables are getting mixed up,
> any idea??
>
> Here is the code you sent me and the zipped files for testing:
>
>
> dat1 <-
> read.table("mtest.txt",header=TRUE)
> dim(dat1)
>
> dat2 <-
> read.table("1998res_x.txt",header=TRUE)
> dim(dat2)
> m1 <- as.matrix(dat1)
> m2 <- as.matrix(dat2)
> vec1 <-
> paste0(rownames(m1)[row(m1)],colnames(m1)[col(m1)])
> vec2 <- paste0(rownames(m2)[row(m2)],colnames(m2)[col(m2)])
> indx <- match(vec1,vec2)
> indx1 <- indx[!is.na(indx)]
> indx2 <- match(vec2,vec1)
> indx2N <- indx2[!is.na(indx2)]
> m1[indx2N] <- m2[indx1]
>
> Thanks a lot!!
>
>
> Best,
>
>
> Elio
>

	[[alternative HTML version deleted]]


