How to present a list of points in R

I work with a large list of points (each point has three dimensions x, y, z).

I am new to R, so I would like to know what is the best way to present such information. As far as I know, the array allows me to represent any multidimensional data, so currently I'm using:

> points<-array( c(1,2,0,1,3,0,2,4,0,2,5,0,2,7,0,3,8,0), dim=c(3,6) )
> points
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    2    2    2    3  -- x dim
[2,]    2    3    4    5    7    8  -- y dim
[3,]    0    0    0    0    0    0  -- z dim

The goal is to perform some calculations to calculate the Euclidean distance between two sets of points, such as:

points1<-array( c(1,2,0,1,3,0,2,4,0,2,5,0,2,7,0,3,8,0), dim=c(3,6) )
points2<-array( c(2,2,0,1,4,0,2,3,0,2,4,0,2,6,0,2,8,0), dim=c(3,6) )

(any hint in this sense would also be greatly appreciated)

+3
source share
4 answers

Calculating the Euclidean distance between two sets of points stored in this way is easy:

sqrt(colSums((points1 - points2)^2))

. :

sqrt(rowSums((points1 - points2)^2))
+4

, dist. ,

dist(t(points),method = "euclidean")

Dist amap, : ( "" "," "," "," "," "," "," "," "," ")

+4

, , CRAN - .

+2
source

I would suggest working with a portable matrix, otherwise you are likely to end up calling the t () function more than you would otherwise.

Also, this is probably the data structure you want. Of course, you can do this with a data frame, but I think you better not do this in this situation.

+1
source

Source: https://habr.com/ru/post/1743881/


All Articles