Hi R, I have a matrix,> x1=matrix(NA,6,6,dimnames=list(letters[1:6],LETTERS[1:6]))> x1A B C D E F a NA NA NA NA NA NA b NA NA NA NA NA NA c NA NA NA NA NA NA d NA NA NA NA NA NA e NA NA NA NA NA NA f NA NA NA NA NA NA> x2=matrix(rpois(9,1),3,3,dimnames=list(c("b","a","f"),c("D","E","F")))> x2D E F b 0 3 0 a 2 2 1 f 1 0 0 I need to put the values of x2 in the corresponding elements of x1. So then, x1 would look like: A B C D E F a NA NA NA 2 2 1 b NA NA NA 0 3 0 c NA NA NA NA NA NA d NA NA NA NA NA NA e NA NA NA NA NA NA f NA NA NA 1 0 0 What would be the best way to do this? Many Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}}
Perhaps somethink like about this: x1[rownames(x2),colnames(x2)] <- x2 x1 On Mon, Jun 9, 2008 at 8:26 AM, Shubha Vishwanath Karanth < shubhak@ambaresearch.com> wrote:> Hi R, > > > > I have a matrix, > > > > > x1=matrix(NA,6,6,dimnames=list(letters[1:6],LETTERS[1:6])) > > > x1 > > A B C D E F > > a NA NA NA NA NA NA > > b NA NA NA NA NA NA > > c NA NA NA NA NA NA > > d NA NA NA NA NA NA > > e NA NA NA NA NA NA > > f NA NA NA NA NA NA > > > > > x2=matrix(rpois(9,1),3,3,dimnames=list(c("b","a","f"),c("D","E","F"))) > > > x2 > > D E F > > b 0 3 0 > > a 2 2 1 > > f 1 0 0 > > > > > > I need to put the values of x2 in the corresponding elements of x1. So > then, x1 would look like: > > A B C D E F > > a NA NA NA 2 2 1 > > b NA NA NA 0 3 0 > > c NA NA NA NA NA NA > > d NA NA NA NA NA NA > > e NA NA NA NA NA NA > > f NA NA NA 1 0 0 > > > > > > > > What would be the best way to do this? > > > > Many Thanks, > > Shubha > > > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
try this: x1 <- matrix(NA, 6, 6, dimnames = list(letters[1:6], LETTERS[1:6])) x2 <- matrix(rpois(9, 1), 3, 3, dimnames = list(c("b","a","f"), c("D","E","F"))) x1[rownames(x2), colnames(x2)] <- x2 x1 I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Shubha Vishwanath Karanth" <shubhak at ambaresearch.com> To: <r-help at stat.math.ethz.ch> Sent: Monday, June 09, 2008 1:26 PM Subject: [R] Overlaying the matrices> Hi R, > > > > I have a matrix, > > > >> x1=matrix(NA,6,6,dimnames=list(letters[1:6],LETTERS[1:6])) > >> x1 > > A B C D E F > > a NA NA NA NA NA NA > > b NA NA NA NA NA NA > > c NA NA NA NA NA NA > > d NA NA NA NA NA NA > > e NA NA NA NA NA NA > > f NA NA NA NA NA NA > > > >> x2=matrix(rpois(9,1),3,3,dimnames=list(c("b","a","f"),c("D","E","F"))) > >> x2 > > D E F > > b 0 3 0 > > a 2 2 1 > > f 1 0 0 > > > > > > I need to put the values of x2 in the corresponding elements of x1. > So > then, x1 would look like: > > A B C D E F > > a NA NA NA 2 2 1 > > b NA NA NA 0 3 0 > > c NA NA NA NA NA NA > > d NA NA NA NA NA NA > > e NA NA NA NA NA NA > > f NA NA NA 1 0 0 > > > > > > > > What would be the best way to do this? > > > > Many Thanks, > > Shubha > > > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Suppose if I modify the question as:> xA B C D E a 0 0 0 0 1 b 0 1 0 2 1 c 1 2 0 1 2 d 0 0 0 2 0 e 0 1 2 0 2> yD E F b 2 1 2 a 4 0 1 f 1 4 1 I need to get a matrix, which has the dimension being the union of the row names and column names of both x and y such that the new matrix 'xx' should contain only the corresponding elements in x, rest all should be NA. Any ideas for this? Thanks, Shubha -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: Monday, June 09, 2008 4:57 PM To: r-help at stat.math.ethz.ch Subject: [R] Overlaying the matrices Hi R, I have a matrix,> x1=matrix(NA,6,6,dimnames=list(letters[1:6],LETTERS[1:6]))> x1A B C D E F a NA NA NA NA NA NA b NA NA NA NA NA NA c NA NA NA NA NA NA d NA NA NA NA NA NA e NA NA NA NA NA NA f NA NA NA NA NA NA> x2=matrix(rpois(9,1),3,3,dimnames=list(c("b","a","f"),c("D","E","F")))> x2D E F b 0 3 0 a 2 2 1 f 1 0 0 I need to put the values of x2 in the corresponding elements of x1. So then, x1 would look like: A B C D E F a NA NA NA 2 2 1 b NA NA NA 0 3 0 c NA NA NA NA NA NA d NA NA NA NA NA NA e NA NA NA NA NA NA f NA NA NA 1 0 0 What would be the best way to do this? Many Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} ______________________________________________ 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. This e-mail may contain confidential and/or privileged i...{{dropped:10}}