In R, you can perform clustering analysis, including k-means clustering and hierarchical clustering. Here are some examples:
## k-means clustering
# Load the cluster package
library(cluster)
# Generate some data
set.seed(123)
x <- matrix(rnorm(200), ncol = 2)
# Perform k-means clustering
k <- 3
model <- kmeans(x, k)
summary(model) # View the model summary
# Plot the data and the clusters
plot(x, col = model$cluster)
points(model$centers, col = 1:k, pch = 8, cex = 2)
In this code, we first load the `cluster` package. We then generate some data using the `matrix()` function and perform k-means clustering using the `kmeans()` function, specifying the number of clusters as `k = 3`. We can view a summary of the model using the `summary()` function. We then plot the data and the clusters using the `plot()` and `points()` functions, specifying the cluster assignments as colors for the `col` argument and the cluster centers as points with solid squares for the `points()` function.
## Hierarchical clustering
# Generate some data
set.seed(123)
x <- matrix(rnorm(200), ncol = 2)
# Perform hierarchical clustering
model <- hclust(dist(x))
summary(model) # View the model summary
# Plot the dendrogram
plot(model)
In this code, we first generate some data using the `matrix()` function. We then perform hierarchical clustering using the `hclust()` function, specifying the distance matrix as the output of the `dist()` function applied to the data. We can view a summary of the model using the `summary()` function, but for hierarchical clustering, the summary is usually the dendrogram plot. We can visualize the dendrogram using the `plot()` function applied to the model object.
Note that there are many other types of clustering algorithms available in R, including density-based clustering and model-based clustering. You can find more information on these functions in the R documentation or online tutorials and resources.