calculate distances of various points from center point(store)

set.seed(15)
x=runif(10,1,15)      # random x coord
y=runif(10,1,15)      # random y coord
a=runif(10,1,15)
b=runif(10,1,15)
x=as.integer(x)
y=as.integer(y)
a=as.integer(a)
b=as.integer(b)
X=7.5
Y=7.5
store=cbind(X,Y)
onlineloc=cbind(x,y)
instore=cbind(a,b)
plot(x,y,main="Online and instore customer loc",col=" red")
points(a,b,pch=2)
points(X,Y,pch=15)
distance=dist(instore)
distance

it calculate the inter distances in lower pyramid but i need to calculate distances of instore and onlineloc from center point(store) please guide

Are you talking about spatial distances? If so sf::st_distance does just that, and it is vectorized, making it easy to calculate distance from a common reference for an entire vector of points. It can even handle point to polygon distance calculation.

2 Likes

i want to calculate euclidean distance please guide

That functions offers Euclidian distance. Have a look at the documentation, particularly the which argument to get things right.

1 Like
set.seed(15)
x=runif(10,1,15)      # random x coord
y=runif(10,1,15)      # random y coord
a=runif(10,1,15)
b=runif(10,1,15)
x=as.integer(x)
y=as.integer(y)
a=as.integer(a)
b=as.integer(b)
X=7.5
Y=7.5
store=cbind(X,Y)
onlineloc=cbind(x,y)
instore=cbind(a,b)
plot(x,y,main="Online and instore customer loc",col=" red")
points(a,b,pch=2)
points(X,Y,pch=15)
st_distance(instore,store, by_element = F)

it is giving error could not understand the st_distance function well i guess please help

set.seed(15)
  x=runif(10,1,15)      # random x coord
  y=runif(10,1,15)      # random y coord
  x=as.integer(x)
  y=as.integer(y)
 
  X=7.5
  Y=7.5
  store=cbind(X,Y)
  onlineloc=cbind(x,y)
  
  plot(x,y,main="Online and instore customer loc",col=" red")
  points(X,Y,pch=15)

#' ![](reprex_reprex_files/figure-markdown_strict/reprex-body-1.png)

  
  st_distance(onlineloc,store, by_element = F)
#> Error in st_distance(onlineloc, store, by_element = F): could not find function "st_distance"

there is some error in st function pl guide

set.seed(15)
x=runif(10,1,15) # random x coord
y=runif(10,1,15) # random y coord
x=as.integer(x)
y=as.integer(y)

X=7.5
Y=7.5
store=cbind(X,Y)
onlineloc=cbind(x,y)

plot(x,y,main="Online and instore customer loc",col=" red")
points(X,Y,pch=15)

#' 

st_distance(onlineloc,store, by_element = F)
#> Error in st_distance(onlineloc, store, by_element = F): could not find function "st_distance"

there is some error in st function pl guide

Hi @Emmadadil,
where did you define the function st_distance or which package is it in? I don't see you are attaching any packages...

1 Like