[R] Better way to find distances between points in a set?
carl at witthoft.com
Wed Dec 10 23:11:36 CET 2008
I didn't mean to start a flame war, so first off I apologize for firing
off my second comment.
I think a fair compromise is: I should have at least noted that I got
as far as finding dist() (and which.max()), thus showing "some" effort
on my part before posting my query.
Anyway, I'm happy now because not only did I successfully navigate
mapply, I got some great info on dist and which.* from this mailing list.
Stavros Macrakis wrote:
> On Tue, Dec 9, 2008 at 11:52 PM, Charles C. Berry <cberry at tajo.ucsd.edu>wrote:
> Charles, I am distressed by the nasty tone of your note. Though I certainly
> agree that poor queries on this list are an annoyance, and that the posting
> guide gives useful advice on what to do before posting, I don't think that
> poor queries are a reason to presume bad faith for any but the most flagrant
> correspondents ("here is my problem set, solve it for me"). And though I
> have not been on this list very long, I have been answering questions on
> other lists like this for a long time.
> What's more, it seems to me that Witthoft has a legitimate problem. I tried
> to solve it myself, and here is what I come up with:
> test <- matrix(rnorm(20),ncol=2)
> => Error, though which supports arr.ind, which.max doesn't
> Yes, that is documented, but it violates reasonable expectations.
> => 21 oops, that is a vector index; what about arr.ind?
> Apparently the dist class (return class of dist fnc.)
> doesn't act as an array here (and which/arr.ind is
> happy to treat a vector as an array without warning).
> Is this a problem in dist, or a problem in which?
> So how do I map back from 21 to 3,7? Nothing obvious.
> ? `[`, ? array, and ? dist don't seem to help.
> So let's convert to a matrix explicitly:
> => row col
> [1,] 21 1
> Hmm, that's not right; apparently the == flattened the
> dist class into its base vector, which which/arr.ind
> helpfully (?) treated as a matrix.
> => row col
> 7 7 3
> 3 3 7
> Finally getting useful results; but converting to the array
> added in the upper triangular part... oh, well, we need to
> choose *which* maximum anyway.
> So, as Witthoft said: what's a better, or cleaner way to do all this? And
> why aren't R's operators more regular in their behavior? Why doesn't
> arr.ind work for which.max? Why doesn't dist(x)==1 return an object of the
> same shape as the original dist (as matrix(...)==1 would have), but instead
> flatten it? Where does one find the function that maps from a list of array
> indices to the corresponding vector index and vice versa?
More information about the R-help