# perform cluster on a multiple dimensional data in R

I have a data set which has 2488 samples and each sample has 13 features.Now I want to perform cluster on this data set in R but I found k-means method usually for two dimensions data.So can any one help me? Many thanks!

You can use any number of dimensions for k-means. Therefore, you can use the standard k-means library for R as long as:

• your data contains only metric variables and
• every column is scaled to the same range.

See this related question.

Scale it.

Use k-modes. [R]

### If your data contains categorical and metric variables:

Use k-prototypes. [R]

### How does k-means work?

1. The algorithm randomly initializes k points in your data. These are your cluster centers.
2. It assigns every data point to the nearest cluster center. Usually, this is done by calculating the Euclidian distance of the data point to each of the k cluster centers. Now you have k clusters.
3. It chooses the centroid of each of the k clusters as the new cluster center.
4. The steps 2 and 3 are repeated until the solution converges or the maximum number of steps is reached.

### What this means

The Euclidian distance can be calculated in n-dimensional space. Therefore, you can use any number of dimensions you want.

However, if your data is not scaled, the distance calculations will be different for every feature. Therefore, you need to scale it.

Since Euclidian distance won't make a lot of sense with categorical or with mixed data, you have to use an algorithm that uses Hamming distance instead for these use cases. k-modes uses only Hamming distance. k-prototypes uses Hamming distance for categorical and Euclidian distance for metric variables.

• Thank you for your answer! I'm still wondering how can I visualize the result since the photo can only demonstration 2-d view. what the X and Y axis will be? Commented Aug 7, 2019 at 2:38
• @guohuili Regarding your question: Visualizing multidimensional data is a very interesting and broad topic. I recommend putting this in a separate question as you will get more answers with higher quality then and it will also help others in the future that have a similar question. Commented Aug 7, 2019 at 9:16

K-means is in no way restricted to 2d data.

Hat is just the lecture toy examples because our screens tend to be 2 dimensional, so that is easier to visualize.