"NS introduced by coercion" during cluster analysis in R

Guys I'm new to this language, I do cluster analysis on a data frame, but when I calculate the distance, I get this warning "NAs entered by duress". What does it mean?

d <- dist(as.matrix(mydata1)) Warning message: In dist(as.matrix(mydata1)) : NAs introduced by coercion 

My sample data

 Metafamily Total July cpc July cse_pla July offline July organic xerox 8560 275.829417 0.20943223 0.032628862 0.169210813 0.1130048 office-supplie 246.9125664 0.057833047 0.020209909 0.535358617 0.136165617 

In this case, except for the Metafamily column, all columns are numeric in the class.

Guys, please help me solve this problem.

+7
r cluster-analysis
source share
1 answer

This is the first column that creates the problem:

 > a <- c("1", "2",letters[1:5], "3") > as.numeric(a) [1] 1 2 NA NA NA NA NA 3 Warning message: NAs introduced by coercion 

Inside dist must be a compulsion to the numeric one that the NA generates, as mentioned above.

I suggest applying dist without the first column, or better move it to rownames if possible, because the result will be different:

 > dist(df) 1 2 3 4 2 1.8842186 3 1.9262360 1.2856110 4 3.2137871 1.7322788 2.9838920 5 1.3299455 0.9872963 1.9158079 1.8889050 Warning message: In dist(df) : NAs introduced by coercion > dist(df[-1]) 1 2 3 4 2 1.538458 3 1.572765 1.049697 4 2.624046 1.414400 2.436338 5 1.085896 0.806124 1.564251 1.542284 

btw: you don't need as.matrix when calling dist . It will be one way or another internally.

EDIT: Using Rost Names

 rownames(df) <- df$id > df id var1 var2 AA -0.6264538 -0.8204684 BB 0.1836433 0.4874291 CC -0.8356286 0.7383247 DD 1.5952808 0.5757814 EE 0.3295078 -0.3053884 > dist(df[-1]) # you colud also remove the 1st col at all, using df$id <- NULL. ABCD B 1.538458 C 1.572765 1.049697 D 2.624046 1.414400 2.436338 E 1.085896 0.806124 1.564251 1.542284 
+13
source share

All Articles