R: How to store vectors
I'm trying to write a function to determine the Euclidean distance between X (a point) and Y (a set of n points)
[,1] [,2] [,3] [1,] 0 2 1 [2,] 1 1 1
Which passes points (0,2,1) and (1,1,1) to the function
However, when I pass x as a normal (column) vector, the two variables do not match in the function I either have to transpose X or Y or save the vector in other ways
My question: what is the standard way to save multiple vectors in R? (my matrix Y) is it just my Y transpose or could it be a list or data frame?
Solution
There is no standard method, so you should choose the most effective method. On the other hand, it depends on the appearance of the vector after creation (it is best to avoid any unnecessary conversion) and the speed function itself
I believe in data with columns x, y, and Z Frame should be a good choice; The distance function will be very simple and fast:
d<-function(x,y) sqrt((y$x-x[1])^2+(y$y-x[2])^2+(y$z-x[3])^2)