[R] how to store a vector of vectors
Duncan Murdoch
murdoch.duncan at gmail.com
Sat Nov 13 14:41:57 CET 2010
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
More information about the R-help
mailing list