I would like to process the following matrix data by using which()
, and return all the row names, for example:
m = matrix(seq(-1,1, 0.5), nrow = 3)
# [,1] [,2]
#[1,] -1.0 0.5
#[2,] -0.5 1.0
#[3,] 0.0 -1.0
which(m==0.5,arr.ind=TRUE)
# row col
# [1,] 1 2
How can I get the matrix like this? All row names can be shown in the table, and the missing value in col
is NA.
# row col
# [1,] 1 2
# [2,] 2 NA
# [3,] 3 NA
# [4,] 4 NA
CodePudding user response:
Here is a method that first change the matrix into a dataframe, and use tidyr::complete()
to "expand" the dataframe based on the number of rows of m
. Finally change it back to a matrix.
library(tidyverse)
as.data.frame(which(m==0.5,arr.ind=TRUE)) %>%
complete(row = 1:nrow(m)) %>%
as.matrix()
row col
[1,] 1 2
[2,] 2 NA
[3,] 3 NA
CodePudding user response:
Using only base R
, you could build a function. Please find below a reprex:
Reprex
- Your data
m = matrix(seq(-1,1, 0.5), nrow = 3)
- Code of the function
indexfunct <- function(x, value){
res <- matrix(NA, nrow = dim(x)[1], ncol = dim(x)[2], dimnames = (list(seq(dim(x)[1]), c("row", "col"))))
res[,1] <- seq(dim(x)[1])
res[which(x==value,arr.ind=TRUE)[,1],2] <- which(x==value,arr.ind=TRUE)[,2]
return(res)
}
- Output of the function
indexfunct(m, value = 0.5)
#> row col
#> 1 1 2
#> 2 2 NA
#> 3 3 NA
Created on 2022-03-06 by the reprex package (v2.0.1)
CodePudding user response:
Another possible base R solution:
m = matrix(seq(-1,1, 0.5), nrow = 3)
rbind(which(m == 0.5, arr.ind = TRUE),
cbind(setdiff(1:nrow(m), which(m == 0.5, arr.ind = TRUE)[,"row"]), NA))
#> row col
#> [1,] 1 2
#> [2,] 2 NA
#> [3,] 3 NA
In case a function is needed:
getindexes <- function(mat, value)
{
rbind(which(mat == value, arr.ind = TRUE),
cbind(setdiff(1:nrow(mat), which(mat == value,arr.ind = TRUE)[,"row"]), NA))
}
getindexes(m, 0.5)
#> row col
#> [1,] 1 2
#> [2,] 2 NA
#> [3,] 3 NA
CodePudding user response:
We indeed can try this base R option without which
> v <- rowSums(col(m) * (m == 0.5))
> cbind(row = 1:nrow(m), col = v * NA^(!v))
row col
[1,] 1 2
[2,] 2 NA
[3,] 3 NA