1
$\begingroup$

I have a large raw dataset on crime and I want to cluster the data using k-means, However, I get an Error when I enter this code:

Rawdata.3means <- kmeans(Rawdata, centers = 3).

Error:

Error in kmeans(Rawdata, centers = 3) : 
  more cluster centers than distinct data points.
In addition: Warning message:
In storage.mode(x) <- "double" : NAs introduced by coercion

How can I resolve this error?

$\endgroup$
1
$\begingroup$

Kmeans utilize the mean of your data points for clustering . If your dataset is made of plain text or other type of factors (i.e not numbers) then it wont work for you . You need to do another step of preprocessing your data before you can apply Kmean or most of the ML algorithms .

  1. Categorical dataset : i.e your data is in the form of multiple categories like column of fruits with values of Apple , orange ,banana etc. Then you can use "one hot encoding" method that will transform your category column into multiple columns that each indicate if the sample is belong to the relevant category (i.e for column with 3 fruit types you will get 3 new binary (1 or 0) columns - is apple ? is orange? is banana ? read more about how to do it in R here : One hot encoding in R

Update: like some suggested in the comments , K means wont be the best approach for clustering categorical data and in some cases you can get much better results when using more suitable approaches .Here is a link to another (more advanced) method for clustering categorical data in R - ROCK algorithem (kaggle notebook) . Also ,you can read about "Kmode" which is similar to kmeans for categories and implemented in R

  1. If your dataset is plain text (like tweets or stackexchange posts) : One common method is using td-idf (but there are many more) , you can read more here: Text clustering using R: an introduction for data scientists and here in a nice kaggle R notebook: R : cleaning data, and using TF-IDF
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Maybe you could flesh out your answer a bit more by suggesting what preprocessing could be done to convert strings to a suitable format? $\endgroup$ – HFulcher Feb 22 '19 at 15:13
  • $\begingroup$ Hi, thanks for replying. In my dataset, I have text and some numeric values with plus and pound symbols. This is where I got the data from and its related to crime: old.datahub.io/dataset/uk-criminal-justice/resource/…. $\endgroup$ – jen ki Feb 22 '19 at 16:11
  • $\begingroup$ @jenki , your data set type is categorical data type , i've added to the main answer the common method to handle that type of data. there are more advanced methods but One-hot-encoding is (as far as i know) the most common method for that type of data. $\endgroup$ – Latent Feb 22 '19 at 17:09
  • 1
    $\begingroup$ While you can use one-hot encoding and similar, that usually yields quite poor and uninterpretable results. Using a method that is actually designed for text or factors is better. $\endgroup$ – Has QUIT--Anony-Mousse Feb 23 '19 at 7:50
  • $\begingroup$ @Anony-Mousse i've updated the main answer and added some more advanced methods which can be more beneficial for categorical clustering $\endgroup$ – Latent Feb 23 '19 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.