**Solution :**

I have gone through your question and I have thoroughly understood it.

I had faced the very similar error in past. So I had done the lot of research on it and eventually found out the solution on it. Please follow the below approach to resolve your issue.

The reason behind you are facing that error can be found in below way:

> typeof(data.frame)

[1] "closure"

You are effectively trying to pass in the method that you use to create data frames, instead of the data frame.

The vignette for the broom has a way to make code like your work:

** **

library(dplyr)

set.seed(2019)

centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), x1=c(5, 0, -3), x2=c(-1, 1, -2))

points <- centers %>% group_by(cluster) %>%

do(data.frame(x1=rnorm(.$size[1], .$x1[1]),

x2=rnorm(.$size[1], .$x2[1])))

library(ggplot2)

ggplot(points, aes(x1, x2, color=cluster)) + geom_point()

points.matrix <- cbind(x1 = points$x1, x2 = points$x2)

# Notice the points.matrix defined above

kclusts <- data.frame(k=1:9) %>% group_by(k) %>% do(kclust=kmeans(points.matrix, .$k))