R: How to Get Row and Column Names of The True Elements of a Matrix

R: how to get row and column names of the true elements of a matrix?

You can directly use apply.

apply(
x, 1,
function(u) paste( names(which(u)), collapse="," )
)

Extracting row and column names with its value from matrix

You could use sig_values to subset Pmatrix

cbind.data.frame(colIDs = colnames(Pmatrix)[sig_values[, 1]],
rowIDs = rownames(Pmatrix)[sig_values[, 2]],
values = Pmatrix[sig_values])

# colIDs rowIDs values
#1 MU101188 MU101188 1.000
#2 MU101188 MU101310 0.506
#3 MU101310 MU101310 1.000
#4 MU101326 MU101326 1.000
#5 MU10251 MU101326 0.806
#6 MU10251 MU10251 1.000

r How to get columns names based on a row criteria for ALL rows

We can use apply with MARGIN = 1

data$NewColumn <- apply(data, 1, FUN = function(x) paste(names(x)[x], collapse=' '))

How do I retrieve a matrix column and row name by a matrix index value?

First you need to get the row and column of that index using arrayInd.

k <- arrayInd(4, dim(mdat))

You can then get the right name by getting that element of the row and column names

rownames(mdat)[k[,1]]
colnames(mdat)[k[,2]]

Or both at once using mapply:

mapply(`[[`, dimnames(mdat), k)

Get column and row names of matrix indices in a vector

The which function already provides a nice interface to getting all the row and column indices of a matrix that meet a certain criterion, using the arr.ind=TRUE argument. This is both much less typing and much more efficient than looping through each matrix element. For instance, if you wanted to get all the indices where your matrix equaled 5, you could use:

(idx <- which(mat == 5, arr.ind=TRUE))
# row col
# R1 1 2
# R3 3 4

Now all that remains is a simple lookup using the row and column names of your matrix:

cbind(rownames(mat)[idx[,"row"]], colnames(mat)[idx[,"col"]])
# [,1] [,2]
# [1,] "R1" "C2"
# [2,] "R3" "C4"

You could write this result out to a file using write.csv.

Extracting row and column names from matrix in R

Building on the same steps, we can also use logic as below. (Not sure on inbuilt function to return the row,col as one-shot answer).
Also tested this code with presence of multiple 1's in rows and it works.

Code snippet below:

Pmatrix = read.csv ("file.csv", header = TRUE, row.names = 1)
sig_values <- which(Pmatrix==1, arr.in= TRUE)

# just check the values
Pmatrix
sig_values

# incase there are multiple 1's in Pmatrix row
# or one need to sort the order for the row-wise display
sig_values<-sig_values[order(sig_values[,1]),]
# remove the above line, incase there are no multiple 1's in input file or no sorting is desired

# code to get the desired rowname and colname
i<-1
while (i <= nrow(sig_values)){

# you can use whatever format and store in variabe or do your processing here
# e.g. my format was (row,col), hence the paste format
row_col<-paste("(",dimnames(Pmatrix)[[1]][sig_values[i,1]],",",dimnames(Pmatrix)[[2]][sig_values[i,2]],")")
print(row_col)
i<-i+1
}

#Output
[1] "( MU101188 , MU101188 )"
[1] "( MU101310 , MU101310 )"
[1] "( MU101326 , MU101326 )"
[1] "( MU10251 , MU10251 )"

Extracting col/row names from a matrix based on value condition in R

If we are interested in the row/column names, then convert to table, and coerce it to a data.frame and subset

subset(as.data.frame(as.table(m)), Freq > 5, select = c(Var1, Var2))

data

m <-structure(c(0, 1.6898, 7.55815, 4.18765, 4.4806, 4.41775, 3.9795, 
4.1283, 4.255, 4.4811, 1.6898, 0, 7.67225, 4.113, 4.48225, 4.62525,
3.9288, 4.02495, 4.19675, 4.4686, 7.55815, 7.67225, 0, 7.3129,
7.23675, 7.46935, 7.29925, 7.41055, 7.4329, 7.28585, 4.18765,
4.113, 7.3129, 0, 3.8151, 3.35225, 2.886, 3.29, 3.0194, 3.949,
4.4806, 4.48225, 7.23675, 3.8151, 0, 4.2949, 3.66205, 4.0022,
3.70005, 2.34825, 4.41775, 4.62525, 7.46935, 3.35225, 4.2949,
0, 3.42355, 3.6388, 2.27245, 4.23745, 3.9795, 3.9288, 7.29925,
2.886, 3.66205, 3.42355, 0, 2.48115, 2.97045, 3.6137, 4.1283,
4.02495, 7.41055, 3.29, 4.0022, 3.6388, 2.48115, 0, 2.9763, 3.92015,
4.255, 4.19675, 7.4329, 3.0194, 3.70005, 2.27245, 2.97045, 2.9763,
0, 3.80345, 4.4811, 4.4686, 7.28585, 3.949, 2.34825, 4.23745,
3.6137, 3.92015, 3.80345, 0), .Dim = c(10L, 10L), .Dimnames = list(
c("0", "1", "2", "4", "5", "6", "7", "8", "9", "10"), c("0",
"1", "2", "4", "5", "6", "7", "8", "9", "10")))

create a matrix and row/col names in loop, in R

There are several problems with the code in the question:

  1. It is trying to insert names into the names vectors but no names have been set on the matrix.
  2. colnames(mat)[cc]<-... should be colnames(mat)[cc-3]<-
  3. Using byrow=TRUE is not an error but it is pointless since every element is 0 so it doesn't matter what order 0 is inserted.

1) Suggest doing it this way instead:

rr <- 2:8
cc <- 4:20
mat <- outer(rr, cc)
dimnames(mat) <- list(paste0("class.", rr), paste0("count.", cc))

2) Alternately, this could be done via list comprehensions using the listcompr package. For each value of r in rr the gen.named.vector call creates a named vector forming one row and then gen.named.matrix creates a matrix from the rows.

library(listcompr)
gen.named.matrix("class.{r}", gen.named.vector("count.{c}", c*r, c = cc), r = rr)

3) If you want to fix up the code in the question then do it like this:

mat <- matrix(0, nrow = 7, ncol = 17 ,
dimnames = list(character(7), character(17)))
for (rr in 2:8) {
rownames(mat)[rr-1] <- paste('class', rr, sep='.')
for(cc in 4:20) {
mat[rr-1, cc-3] <- rr * cc
colnames(mat)[cc-3] <- paste('count', cc, sep='.')
}
}


Related Topics



Leave a reply



Submit