[R] merging two maxtrices

Bill.Venables at csiro.au Bill.Venables at csiro.au
Sun Sep 5 09:21:12 CEST 2010


Is this all you want?

> j <- matrix(nrow=10,ncol=10)
> k <- matrix(seq(1:50), ncol=10)
> row.names(k) <- seq(2,10,by=2)
> 
> row.names(j) <- 1:10
> j[row.names(k), ] <- k
> 
> j
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
1    NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
2     1    6   11   16   21   26   31   36   41    46
3    NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
4     2    7   12   17   22   27   32   37   42    47
5    NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
6     3    8   13   18   23   28   33   38   43    48
7    NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
8     4    9   14   19   24   29   34   39   44    49
9    NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
10    5   10   15   20   25   30   35   40   45    50
> 

 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of steven mosher
Sent: Sunday, 5 September 2010 5:10 PM
To: r-help
Subject: [R] merging two maxtrices

  j<-matrix(nrow=10,ncol=10)
  k<-matrix(seq(1:50), ncol=10)
  row.names(k) <- seq(2,10,by=2)
  j
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [2,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [3,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [4,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [5,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [6,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [7,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [8,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [9,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
[10,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
  k
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
2     1    6   11   16   21   26   31   36   41    46
4     2    7   12   17   22   27   32   37   42    47
6     3    8   13   18   23   28   33   38   43    48
8     4    9   14   19   24   29   34   39   44    49
10    5   10   15   20   25   30   35   40   45    50

is there a simple way to merge j and k By the row.names in k

so that row named '2' is placed in the 2nd row of j.. and so forth through
4,6,8,10

the actual example has a sparse k.. not evenly spaced

so this should also be mergeable

 row.names(k) <- c(1,2,5,6,9)
 k
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
1    1    6   11   16   21   26   31   36   41    46
2    2    7   12   17   22   27   32   37   42    47
5    3    8   13   18   23   28   33   38   43    48
6    4    9   14   19   24   29   34   39   44    49
9    5   10   15   20   25   30   35   40   45    50

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list