K means clustering in R - ignoring row id

I have a data frame as follows:

X1 X2 X3 3 102.20000 26.07667 4 115.00000 25.12500 5 36.70000 35.05545 

If column X1 denotes a unique identifier for the row, and X2, X3 are functions

I want to scale before doing k means data clustering,

  mydata <- scale(mydata) X1 X2 X3 -11715.6 -12.2200734 -9.7826627 -11714.6 0.5799266 -10.7343294 -11713.6 -77.7200734 -0.8038748 

I don’t want the X1 column to scale, but I want it to stay on the data frame. Any way to do this?

+5
source share
1 answer

You can mark a unique identifier in the rows of a data frame through their rownames .

 rownames(mydata) = mydata$X1 mydata$X1 = NULL mydata = scale(mydata) 

If you want to execute k-means on scaled data, I will just leave the row names as identifiers for any analysis. You can return them whenever you want with mydata$X1 = rownames(mydata) .

+4
source

All Articles