create a community matrix and use k means to do a cluster analysis and then create a table of the cluster assignments per variable in r

# Create a community matrix
community_data <- data.frame(
    Person1 = c(1, 0, 1, 1),
    Person2 = c(0, 1, 1, 0),
    Person3 = c(1, 1, 0, 1),
    Person4 = c(0, 1, 1, 1)
)

# K-means clustering
cluster_results <- kmeans(community_data, centers = 3)  # You can choose any number of centers

# Create a table of the cluster assignments per variable
cluster_assignments <- data.frame(Person = colnames(community_data), Cluster = cluster_results$cluster)
print(cluster_assignments)
481 chars
15 lines

This code snippet creates a community matrix, performs k-means clustering on the data, and generates a table showing the cluster assignments per variable.

gistlibby LogSnag