DynV Montrealer
2024-May-03 12:47 UTC
[R] Get a copy of a matrix only for TRUE entries of a matching size boolean matrix?
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 Thank you kindly [[alternative HTML version deleted]]
Ben Bolker
2024-May-03 14:24 UTC
[R] Get a copy of a matrix only for TRUE entries of a matching size boolean matrix?
In two steps: result <- matrix(NA_character_, nrow=nrow(mat_letters), ncol =ncol(mat_letters)) result[mat_bools] <- mat_letters[mat_bools] On 2024-05-03 8:47 a.m., DynV Montrealer wrote:> 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 > > Thank you kindly > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Marc Girondot
2024-May-03 14:28 UTC
[R] Get a copy of a matrix only for TRUE entries of a matching size boolean matrix?
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]]