anna
2010-Feb-02 20:07 UTC
[R] Build a matrix from another matrix by specifying the indexes
Hello R specialists, I have a base vector called vBase with 102 elements. I have another vector vec1 which elements are only part of vBase but is shorter. I transform vec1 so I get a vector with the same size as vBase and with each common element on the same indexed row. If a value is missing in vec1 then I put a Na like this: vec1 vBase Amsterdam Amsterdam Na Bruxelles Dublin Dublin Frankfurt Frankfurt Paris Paris This is how I did it: vec1 <- vec1[match(vBase,vec1)] But what I really want to is the same but on a matrix columns, for example mat <- cbind( vec1, vec2 ,vec3) I found a way to make a match on each column: indexes <- apply(mat, 2, match, x = vBase) It returns me the indexes matrix but I don't know how each index column to each mat column like I did for vec1 with vec1[]. Any idea? ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Build-a-matrix-from-another-matrix-by-specifying-the-indexes-tp1460326p1460326.html Sent from the R help mailing list archive at Nabble.com.
Peter Alspach
2010-Feb-02 20:16 UTC
[R] Build a matrix from another matrix by specifying the indexes
Tena koe Anna This sounds like merge to me. ?merge HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of anna > Sent: Wednesday, 3 February 2010 9:07 a.m. > To: r-help at r-project.org > Subject: [R] Build a matrix from another matrix by specifying > the indexes > > > Hello R specialists, > I have a base vector called vBase with 102 elements. I have > another vector > vec1 which elements are only part of vBase but is shorter. I > transform vec1 so I get a vector with the same size as vBase > and with each common element on the same indexed row. If a > value is missing in vec1 then I put a Na like > this: > > vec1 vBase > Amsterdam Amsterdam > Na Bruxelles > Dublin Dublin > Frankfurt Frankfurt > Paris Paris > This is how I did it: vec1 <- vec1[match(vBase,vec1)] But > what I really want to is the same but on a matrix columns, > for example mat <- cbind( vec1, vec2 ,vec3) I found a way to > make a match on each column: > indexes <- apply(mat, 2, match, x = vBase) It returns me the > indexes matrix but I don't know how each index column to each > mat column like I did for vec1 with vec1[]. Any idea? > > ----- > Anna Lippel > -- > View this message in context: > http://n4.nabble.com/Build-a-matrix-from-another-matrix-by-spe > cifying-the-indexes-tp1460326p1460326.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >