Learn R Programming

RcmdrMisc (version 2.10.2)

assignCluster: Append a Cluster Membership Variable to a Dataframe

Description

Append a Cluster Membership Variable to a Dataframe

Usage

assignCluster(clusterData, origData, clusterVec)

Value

A factor (with integer labels) that indicate the cluster assignment for each observation, with an NA value given to observations not used in the clustering solution.

Arguments

clusterData

The data matrix used in the clustering solution. The data matrix may have have only a subset of the observations contained in the original dataframe.

origData

The original dataframe from which the data used in the clustering solution were taken.

clusterVec

An integer variable containing the cluster membership assignments for the observations used in creating the clustering solution. This vector can be created using cutree for clustering solutions generated by hclust or the cluster component of a list object created by kmeans or KMeans.

Author

Dan Putler

Details

Correctly creates a cluster membership variable that can be attached to a dataframe when only a subset of the observations in that dataframe were used to create the clustering solution. NAs are assigned to the observations of the original dataframe not used in creating the clustering solution.

This code originally by Dan Putler, used with permission.

See Also

hclust, cutree, kmeans, KMeans.

Examples

Run this code
## Load USArrests data set
data(USArrests)

## Create three cluster
USArrkm3 <- KMeans(USArrests[USArrests$UrbanPop<66, ], centers=3)

## Create a variable with cluster assignment
assignCluster(USArrests[USArrests$UrbanPop<66, ], USArrests, USArrkm3$cluster)

Run the code above in your browser using DataLab