# Create random coordinates with pre specified degree of clustering

In R, I want to create a random data frame of x and y coordinates, that has a certain degree of clustering. For example 15 $$k$$-means clusters. I currently have this:

customer_locations <- data.frame(
id = 1:n,
x = round(runif(n) * grid_size),
y = round(runif(n) * grid_size)
)

• The phrase "a certain degree of clustering" is a bit vague. Let's say you want 15 clusters. Any set of points can be clustered into 15 clusters. So what specifically do you want out of the data? Do you have a specific criterion for what would make the data adequately clustered?
– prubin
Jun 27, 2022 at 15:36

## 1 Answer

I am asking myself the same as @prubin. In case you mean with "degree" how far the clusters are from each other (and therefore also how well they are seperatable), then this might help:

num_clusters = 5
points_per_cluster = 10
degree = 20
allPoints = list()
for (i in 1:num_clusters){
offsets = runif(2) * degree
x_coord = runif(points_per_cluster) + offsets[1]
y_coord = runif(points_per_cluster) + offsets[2]
allPoints = rbind(allPoints, cbind(x_coord, y_coord))
}
plot(allPoints)


A degree of 5 would give you sth like this:

A degree of 20 would give you sth like this:

• Yes this is perfect! Thanks! Apologies for the vague explanation Jun 28, 2022 at 6:06
• Use e.g. min-max normalization to scale all values between 0 and 1 Jul 2, 2022 at 10:02