>>>>> DynV Montrealer >>>>> on Fri, 3 May 2024 10:39:10 -0400 writes:
> It's exactly what I was looking for, thanks. I'm replying > to the whole list so others can skip this question, to not > waste time on it. good. > Bonne fin de journée de Montréal (nous sommes le matin ici) Note that the answer Ben Bolker gave you (using "indexing") is much more efficient than the ifelse() answer below. ifelse() may be convenient .. but is almost always suboptimal, where as "indexing" is typically very often more efficient, although often requiring a bit more thought than ifelse() [so, if overall efficience is almost entirely your own human time, then using ifelse() may still be a good idea... ] Martin > On Fri, May 3, 2024 at 10:30 AM Marc Girondot <marc_...@yahoo.fr> wrote: >> Is it what you want ? >> mat_letters <- matrix(data=c('A', 'B', 'C', 'D'), ncol=2, byrow=TRUE) >> mat_bools <- matrix(data=c(FALSE, TRUE, TRUE, FALSE), ncol=2, byrow=TRUE) >> >> ifelse(mat_bools, mat_letters, "") >> ifelse(mat_bools, mat_letters, NA) >> >> > ifelse(mat_bools, mat_letters, "") >> [,1] [,2] >> [1,] "" "B" >> [2,] "C" "" >> > ifelse(mat_bools, mat_letters, NA) >> [,1] [,2] >> [1,] NA "B" >> [2,] "C" NA >> >> Marc >> >> >> >> Le 03/05/2024 à 14:47, DynV Montrealer a écrit : >> >> Is there a way to get a copy of a matrix only for TRUE entries of a >> matching size boolean matrix? For **example**: >> >> mat_letters <- matrix(data=c('A', 'B', 'C', 'D'), ncol=2, byrow=TRUE) >> mat_letters >> >> [,1] [,2] >> [1,] "A" "B" >> [2,] "C" "D" >> >> mat_bools <- matrix(data=c(FALSE, TRUE, TRUE, FALSE), ncol=2, byrow=TRUE) >> mat_bools >> >> [,1] [,2] >> [1,] FALSE TRUE >> [2,] TRUE FALSE >> *Reminder:* The following is only an example ; the solution might look very >> different. >> some_command(mat_letters, mat_bools, false=empty) >> [,1] [,2] >> [1,] "" "B" >> [2,] "C" "" >> some_command(mat_letters, mat_bools, false=na) >> [,1] [,2] >> [1,] NA "B" >> [2,] "C" NA >> >> >> > [[alternative HTML version deleted]] > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.