Get row and column indices of matches using which

0 votes

Say I have some matrix, for example:

m = matrix(rep(c(0, 0, 1), 4), nrow = 4)
m
     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    0    1    0
[3,]    1    0    0
[4,]    0    0    1

If I run which, I get list of normal indices:

> which(m == 1)
[1]  3  6  9 12

I want to get something like matrix indices - each index containing the row and column number:

     [,1] [,2]
[1,]    3    1
[2,]    2    2
[3,]    1    3
[4,]    4    3

Is there any simple function to do this? Moreover, it should somehow contain the row and column names:

> rownames(m) = letters[1:4]
> colnames(m) = letters[5:7]
> m
  e f g
a 0 0 1
b 0 1 0
c 1 0 0
d 0 0 1

but I don't now how, maybe like

     [,1] [,2] [,3] [,4]
[1,]    3    1    c    e
[2,]    2    2    b    f
[3,]    1    3    a    g
[4,]    4    3    d    g

or, maybe return 2 vectors (for rows and columns), like

c b a d
3 2 1 4

e f g g
1 2 3 3
Jul 5, 2022 in Data Science by avinash
• 1,840 points
332 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Data Science

0 votes
1 answer

Changing column names of a data frame

import pandas as pd #define DataFrame df = pd.DataFrame({'team':['A', ...READ MORE

answered Jun 24, 2022 in Data Science by Sohail
• 3,040 points
550 views
0 votes
1 answer

grepl in R to find matches to any of a list of character strings

Inside of a grepl regular expression, you ...READ MORE

answered Jun 24, 2022 in Data Science by Sohail
• 3,040 points
3,254 views
0 votes
0 answers

How do I get a list of built-in data sets in R?

Please advise on how to obtain the ...READ MORE

Jul 5, 2022 in Data Science by avinash
• 1,840 points
313 views
0 votes
0 answers

Understanding of minbucket function in CART model using R

Let's say that the training data is ...READ MORE

Jul 20, 2022 in Data Science by avinash
• 1,840 points
617 views
0 votes
0 answers

The difference of na.rm and na.omit in R

I've just started with R and I've ...READ MORE

Jul 22, 2022 in Data Science by avinash
• 1,840 points
756 views
0 votes
1 answer

Converting xlsx to xls using Microsoft Office Compitablity Pack's excelcnv

Simply rearranging the positions of the parameters ...READ MORE

answered Mar 25, 2022 in Data Science by gaurav
• 23,260 points
1,321 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,049 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,227 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,912 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,158 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP