My Coyne
2008-Feb-13 19:07 UTC
[R] How to handle Which on two matrices that do not have same number of rows
R-newbie question I have 2 matrices (a) P1 has only one column of 32K rows (b) PC has 2 column {P, C} of 3200 rows Every values in P1 matches with a value in PC[,p] (column p). I would like to use Which to search for all value in P1 that matchex PC[,p] and get the PC[,c]. However because P1 and PC does not have the same number of rows, I got lots of 'NA'. Thanks for your help. Example.. P1 <- {'p001', 'p001', 'p002', 'p010'..} PC <- { c('p001','class a'), c('p002', 'class b'),.. , c('p010', 'class 10')} Result <- {c('p001', 'class a'), c('p001', 'class a'), c ('p002', 'class b'), c('p010', 'class 10')...} My D. Coyne [[alternative HTML version deleted]]
Henrique Dallazuanna
2008-Feb-13 19:40 UTC
[R] How to handle Which on two matrices that do not have same number of rows
Try this: merge(data.frame(P1), data.frame(PC), by.x=1, by.y=1) On 13/02/2008, My Coyne <mcoyne at boninc.com> wrote:> R-newbie question > > > > I have 2 matrices > > (a) P1 has only one column of 32K rows > > (b) PC has 2 column {P, C} of 3200 rows > > > > Every values in P1 matches with a value in PC[,p] (column p). I would like > to use Which to search for all value in P1 that matchex PC[,p] and get the > PC[,c]. However because P1 and PC does not have the same number of rows, I > got lots of 'NA'. Thanks for your help. > > > > Example.. > > > > P1 <- {'p001', 'p001', 'p002', 'p010'..} > > PC <- { c('p001','class a'), c('p002', 'class b'),.. , c('p010', 'class > 10')} > > > > Result <- {c('p001', 'class a'), c('p001', 'class a'), c ('p002', 'class > b'), c('p010', 'class 10')...} > > > > > > My D. Coyne > > > > > > > > > [[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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Yinghai Deng
2008-Feb-13 21:16 UTC
[R] How to handle Which on two matrices that do not have samenumber of rows
How about this? PC[na.omit(match(P1[,1],PC[,1])),] HTH, DYH -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]On Behalf Of My Coyne Sent: February 13, 2008 2:07 PM To: r-help at stat.math.ethz.ch Subject: [R] How to handle Which on two matrices that do not have samenumber of rows R-newbie question I have 2 matrices (a) P1 has only one column of 32K rows (b) PC has 2 column {P, C} of 3200 rows Every values in P1 matches with a value in PC[,p] (column p). I would like to use Which to search for all value in P1 that matchex PC[,p] and get the PC[,c]. However because P1 and PC does not have the same number of rows, I got lots of 'NA'. Thanks for your help. Example.. P1 <- {'p001', 'p001', 'p002', 'p010'..} PC <- { c('p001','class a'), c('p002', 'class b'),.. , c('p010', 'class 10')} Result <- {c('p001', 'class a'), c('p001', 'class a'), c ('p002', 'class b'), c('p010', 'class 10')...} My D. Coyne [[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.