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)
  • $\begingroup$ 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? $\endgroup$
    – prubin
    Commented Jun 27, 2022 at 15:36

1 Answer 1


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))

A degree of 5 would give you sth like this:

enter image description here

A degree of 20 would give you sth like this:

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.