I have a 4x4 matrix and I want to identify elements of this matrix that are equal to a specific value such as 1. I want to save the indices of these elements as well as column and row names to two separate vectors. Finally, I want to write all of this information to a txt file.
I managed to get the indices to a txt file but I have no idea how to retrieve the column and row names from the matrix. To test, I am using following example:
mat <- matrix(c(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6), ncol=4, nrow=4)
colnames(mat) <- c("C1","C2","C3","C4")
rownames(mat) <- c("R1", "R2","R3","R4")
r.indices <- c()
c.indices <- c()
for (row in 1:nrow(mat)){
for (col in 1:(ncol(mat)-row+1)){
if (mat[row,col] == cutoff){
#print("this is one!")
r.indices <- c(r.indices,row)
c.indices <- c(c.indices,col)
}
}
}
write.csv(cbind(r.indices, c.indices), file="data.txt")