How to get row and column number of cell that matches a given value in data.frame or distance matrix?

Azrael

When I have a distance matrix (or a data frame based on a matrix), how do I get the row and column that corresponds to a given value?

Example:

df <- data.frame(x = c(11:20), y= c(12:21))
dst <- dist(df)

Output:

          1         2         3         4         5         6         7         8         9
2   1.414214                                                                                
3   2.828427  1.414214                                                                      
4   4.242641  2.828427  1.414214                                                            
5   5.656854  4.242641  2.828427  1.414214                                                  
6   7.071068  5.656854  4.242641  2.828427  1.414214                                        
7   8.485281  7.071068  5.656854  4.242641  2.828427  1.414214                              
8   9.899495  8.485281  7.071068  5.656854  4.242641  2.828427  1.414214                    
9  11.313708  9.899495  8.485281  7.071068  5.656854  4.242641  2.828427  1.414214          
10 12.727922 11.313708  9.899495  8.485281  7.071068  5.656854  4.242641  2.828427  1.414214

Now I want to input e.g. 11.313708 and get as output (9, 1)

akrun

We convert to a matrix and get the index with which with arr.ind=TRUE (assuming that it is what you wanted).

 m1 <- as.matrix(dst)
 which(m1==val, arr.ind=TRUE)

Otherwise, we can use the regular subsetting by row, column if we already know the index of the value. As @nicola mentioned in the comments, there is a chance for floating point issues. To avoid that may be round it and then do the comparison. i.e.

 which(round(m1, 3)== 11.314, arr.ind=TRUE)
 #    row col
 #9    9   1
 #10  10   2
 #1    1   9
 #2    2  10

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get row and column number of cell that matches a given value in data.frame or distance matrix?

From Dev

In an R data frame, for a given row, how can I find what percentage a value in column A is of a value in column B?

From Dev

pandas how get row index by data frame column value

From Dev

Get a row group and column group value in an SSRS Matrix detail cell

From Dev

How to get cell with row and column number by referencing to cell

From Dev

Delete row if value in column matches a fixed cell

From Dev

how to get the position number of a column in a data frame

From Dev

how to get the position number of a column in a data frame

From Dev

Replace value in selective column that matches in data frame

From Dev

Get the row or column of the cell with a given value within a given 2D range

From Dev

How to get a empty value column count in a given row in My SQL

From Dev

How to get the cell value by using the row and column positions using jquery

From Dev

How to get the cell value by using the row and column positions using jquery

From Dev

Return highest number value in column cell plus a related row cell data

From Dev

How to do a sum of a column if a particular cell of each row matches a particular value?

From Dev

How to get the first row of the first column in transformed data frame

From Dev

A cell contains the number of row and I want to get the value of that row on specific column

From Dev

R - How to get row & column subscripts of matched elements from a distance matrix

From Dev

Get labels of distance matrix cell

From Dev

R: return row and column numbers of matches in a data frame

From Java

Pandas - Get first row value of a given column

From Dev

Get row number data frame R

From Dev

Get value in column from matrix of row numbers

From Dev

How to check if a column exists in a matrix or data frame?

From Dev

How can I get an Excel cell given column and row numbers in Applescript?

From Dev

How to split a data frame into a list of data frame given column names?

From Dev

How to get rows by a specific value of the last data frame column

From Dev

Get the column number per row containing specific character in R data frame

From Dev

Get the column number per row containing specific character in R data frame

Related Related

  1. 1

    How to get row and column number of cell that matches a given value in data.frame or distance matrix?

  2. 2

    In an R data frame, for a given row, how can I find what percentage a value in column A is of a value in column B?

  3. 3

    pandas how get row index by data frame column value

  4. 4

    Get a row group and column group value in an SSRS Matrix detail cell

  5. 5

    How to get cell with row and column number by referencing to cell

  6. 6

    Delete row if value in column matches a fixed cell

  7. 7

    how to get the position number of a column in a data frame

  8. 8

    how to get the position number of a column in a data frame

  9. 9

    Replace value in selective column that matches in data frame

  10. 10

    Get the row or column of the cell with a given value within a given 2D range

  11. 11

    How to get a empty value column count in a given row in My SQL

  12. 12

    How to get the cell value by using the row and column positions using jquery

  13. 13

    How to get the cell value by using the row and column positions using jquery

  14. 14

    Return highest number value in column cell plus a related row cell data

  15. 15

    How to do a sum of a column if a particular cell of each row matches a particular value?

  16. 16

    How to get the first row of the first column in transformed data frame

  17. 17

    A cell contains the number of row and I want to get the value of that row on specific column

  18. 18

    R - How to get row & column subscripts of matched elements from a distance matrix

  19. 19

    Get labels of distance matrix cell

  20. 20

    R: return row and column numbers of matches in a data frame

  21. 21

    Pandas - Get first row value of a given column

  22. 22

    Get row number data frame R

  23. 23

    Get value in column from matrix of row numbers

  24. 24

    How to check if a column exists in a matrix or data frame?

  25. 25

    How can I get an Excel cell given column and row numbers in Applescript?

  26. 26

    How to split a data frame into a list of data frame given column names?

  27. 27

    How to get rows by a specific value of the last data frame column

  28. 28

    Get the column number per row containing specific character in R data frame

  29. 29

    Get the column number per row containing specific character in R data frame

HotTag

Archive