Hi, I'm trying to write a function to determine the euclidean distance between x (one point) and y (a set of n points). How should I pass y to the function? Until now, I used a matrix like that: | [,1] [,2] [,3] [1,] 0 2 1 [2,] 1 1 1 | Which would pass the points (0,2,1) and (1,1,1) to that function. However, when I pass x as a normal (column) vector, the two variables don't match in the function. I either have to transpose x or y, or save a vector of vectors an other way. My question: What is the standard way to save more than one vector in R? (my matrix y) Is it just my y transposed or maybe a list or something I don't yet know? Thanks in advance, Alex [[alternative HTML version deleted]]
I at least would need to see an actual example of your code to be able to answer your question. But why not just use dist() and take the appropriate column of the resultant matrix? mydist <- function(x, amat) { # x is the single variable as a vector # amat is the remaining variables as rows alldist <- dist(rbind(x, amat)) as.matrix(alldist)[-1,1] } Sarah On Sat, Nov 13, 2010 at 8:22 AM, Alexx Hardt <mikrowelle1234 at gmx.de> wrote:> Hi, > I'm trying to write a function to determine the euclidean distance > between x (one point) and y (a set of n points). How should I pass y to > the function? Until now, I used a matrix like that: > > | ? ? ?[,1] ?[,2] ?[,3] > [1,] ? ? ?0 ? ? ?2 ? ? ?1 > [2,] ? ? ?1 ? ? ?1 ? ? ?1 > | > > Which would pass the points (0,2,1) and (1,1,1) to that function. > > However, when I pass x as a normal (column) vector, the two variables > don't match in the function. I either have to transpose x or y, or save > a vector of vectors an other way. > > My question: What is the standard way to save more than one vector in R? > (my matrix y) > Is it just my y transposed or maybe a list or something I don't yet know? > > Thanks in advance, > ?Alex > > > ? ? ? ?[[alternative HTML version deleted]] >-- Sarah Goslee http://www.functionaldiversity.org
Alexx Hardt wrote:> Hi, > I'm trying to write a function to determine the euclidean distance > between x (one point) and y (a set of n points). How should I pass y to > the function? Until now, I used a matrix like that: > > | [,1] [,2] [,3] > [1,] 0 2 1 > [2,] 1 1 1 > | > > Which would pass the points (0,2,1) and (1,1,1) to that function. > > However, when I pass x as a normal (column) vector, the two variables > don't match in the function. I either have to transpose x or y, or save > a vector of vectors an other way. > > My question: What is the standard way to save more than one vector in R? > (my matrix y) > Is it just my y transposed or maybe a list or something I don't yet know?If all vectors are of the same type and length, a matrix is probably best. There are some obscure situations where it is more efficient to store them as columns rather than rows, but it rarely makes a detectable difference, so you should choose the orientation to match the way you plan to use the data. I don't know how you are constructing x as a column vector. Normally vectors in R are neither columns nor rows: they just have a length. So if your matrix y is as shown, y[1,] will give a plain vector that should be the same shape as x <- c(1,2,3). Duncan Murdoch