Dear R users; Is it possible to get the row and column number of a particular entry in a dist object? Let's say that I want to find the position of the value 1.1837 (the last entry on the dist object below), that is [6,3]. Can I get those values without transforming the object to a matrix?, i.e. working with the dist object only. 1 2 3 2 0.23935864 3 0.56655914 0.71923104 4 0.15272561 0.37926989 0.43931332 5 0.17728654 0.13355685 0.73025495 6 0.61783536 0.52055379 1.18374889 Thanks for any idea PM
Francisco J. Zagmutt
2007-May-11 21:42 UTC
[R] how to get column/row info from a dist object?
But the dist object is not structured with rows and columns. i.e. x=1:4 d=dist(x) 1 2 3 2 1 3 2 1 4 3 2 1 str(d) Class 'dist' atomic [1:6] 1 2 3 1 2 1 ..- attr(*, "Size")= int 4 ..- attr(*, "Diag")= logi FALSE ..- attr(*, "Upper")= logi FALSE ..- attr(*, "method")= chr "euclidean" ..- attr(*, "call")= language dist(x = x) So, AFAIK if you want to get references for rows and columns you need to make it an object that indeed has rows and columns, i.e. a matrix. See ?which to obtain the reference you want in a matrix. Regards, Francisco Pedro Mardones wrote:> Dear R users; > > Is it possible to get the row and column number of a particular entry > in a dist object? > > Let's say that I want to find the position of the value 1.1837 (the > last entry on the dist object below), that is [6,3]. Can I get those > values without transforming the object to a matrix?, i.e. working with > the dist object only. > > 1 2 3 > 2 0.23935864 > 3 0.56655914 0.71923104 > 4 0.15272561 0.37926989 0.43931332 > 5 0.17728654 0.13355685 0.73025495 > 6 0.61783536 0.52055379 1.18374889 > > Thanks for any idea > PM > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
which(..., arr.ind=TRUE) On 5/11/07, Pedro Mardones <mardones.p at gmail.com> wrote:> Dear R users; > > Is it possible to get the row and column number of a particular entry > in a dist object? > > Let's say that I want to find the position of the value 1.1837 (the > last entry on the dist object below), that is [6,3]. Can I get those > values without transforming the object to a matrix?, i.e. working with > the dist object only. > > 1 2 3 > 2 0.23935864 > 3 0.56655914 0.71923104 > 4 0.15272561 0.37926989 0.43931332 > 5 0.17728654 0.13355685 0.73025495 > 6 0.61783536 0.52055379 1.18374889 > > Thanks for any idea > PM > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Gabor Grothendieck
2007-May-12 02:03 UTC
[R] how to get column/row info from a dist object?
On 5/11/07, Pedro Mardones <mardones.p at gmail.com> wrote:> Dear R users; > > Is it possible to get the row and column number of a particular entry > in a dist object? > > Let's say that I want to find the position of the value 1.1837 (the > last entry on the dist object below), that is [6,3]. Can I get those > values without transforming the object to a matrix?, i.e. working with > the dist object only. > > 1 2 3 > 2 0.23935864 > 3 0.56655914 0.71923104 > 4 0.15272561 0.37926989 0.43931332 > 5 0.17728654 0.13355685 0.73025495 > 6 0.61783536 0.52055379 1.18374889 > > Thanks for any idea > PMTry this. row.col <- function(dd, value) { if (length(value) == 1) { g <- grep(value, dd) N <- attr(dd, "Size") idx <- cumsum(seq(N-1, 1)) ic <- sum(g > idx) + 1 ir <- g - c(0,idx)[ic] + ic c(row = ir, col = ic) } else sapply(value, row.col, dd = dd) } # test set.seed(1) x <- matrix(rnorm(100), nrow=5) dd <- dist(x) dd dd[7] row.col(dd, dd[7]) row.col(dd, unlist(dd))