r - Distance origin to point in the space -


i want equalize distance origin points, points given data frame 2 coordinates.

i have points as:

           x      y   1      0.0    0.0   2     -4.0   -2.8   3     -7.0   -6.5   4     -9.0  -11.1   5     -7.7  -16.9   6     -4.2  -22.4   7     -0.6  -27.7   8      3.0  -32.5   9      5.6  -36.7   10     8.4  -40.8 

to distance apply euclidean distance vector. have tried this:

distance <- function(trip) {      distance = lapply(trip, function (x) sqrt( (trip[x,]-trip[1,] )^2+ trip[,x]-trip[,1] )^2))      return(distance)  } 

and well:

distance = apply(trip,1, function (x) sqrt( (trip[x,]-trip[1,] )^2+ (trip[,x]-trip[,1] )^2)) return(distance) 

there's no need loop through individual rows of data apply function. can compute distances in 1 shot vectorized arithmetic in r:

(distance <- sqrt((trip$x - trip$x[1])^2 + (trip$y - trip$y[1])^2)) #  [1]  0.000000  4.882622  9.552487 14.290206 18.571484 22.790349 27.706497 32.638168 37.124790 41.655732 

computing distances @ once vectorized operations quicker in cases have many points.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -