[R] selecting vector elements using matrices and combining the results
Steve Lianoglou
mailinglist.honeypot at gmail.com
Tue Jul 28 19:31:52 CEST 2009
Hi,
On Jul 28, 2009, at 12:48 PM, Jean V Adams wrote:
> I've been scratching my head over this one for too long. I'm hoping
> someone out there can solve this riddle.
>
> I have two vectors of characters, v1 and v2, both of length L, and two
> matrices of logicals, m1 and m2, both of dimension N*L. The first
> matrix
> of logicals corresponds to the first vector of characters, and the
> second
> to the second.
>
> The matrices are telling me which of the elements of v1 and v2 are
> selected, and for each of the L elements either a value from v1 is
> selected, a value from v2 is selected, or neither is selected (but
> both
> are never selected). So, the two matrices never have a TRUE in the
> same
> place, i.e., m1 + m2 yields a matrix of 0s and 1s (no 2s).
>
> What I would like to end up with is a list of length N, that has the
> selected elements of v1 and v2 meshed together in their original
> order.
>
> Here's an example (using much smaller N and L than in my real data):
>
> v1 <- LETTERS[1:6]
> v2 <- letters[1:6]
>
> m1 <- matrix(c(T, F, F, T, F, F, F, F, T, F, T, F, F, F, F, T, F, F),
> byrow=T, ncol=6)
> m2 <- matrix(c(F, T, F, F, F, T, F, F, F, F, F, F, F, F, F, F, T, F),
> byrow=T, ncol=6)
>
> v1
> v2
> m1
> m2
> m1+m2
>
> Given these two vectors and two matrices, I want to end up with this
> list:
>
> result <- list(c("A", "b", "D", "f"), c("C", "E"), c("D", "e"))
> result
I'll leave the part to correct ordering up to you:
lapply(seq(nrow(m1)), function (idx) c(v1[m1[idx,]], v2[m2[idx,]]))
[[1]]
[1] "A" "D" "b" "f"
[[2]]
[1] "C" "E"
[[3]]
[1] "D" "e"
--
Steve Lianoglou
Graduate Student: Computational Systems Biology
| Memorial Sloan-Kettering Cancer Center
| Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
More information about the R-help
mailing list