d <- read.csv(‘https://stats.dip.jp/01_ds/data/Mall_Customers.csv’) colnames(d) <- c(‘id’, ‘gender’, ‘age’, ‘income’, ‘score’)

library(DT) datatable(d, options = list(pageLength = 5))

NGROUPS <- 5 matplot(x = d\(income, y = d\)score, pch = 16, type = ‘p’) grid() # カラーパレット COL <- rainbow(NGROUPS)

km <- kmeans(d[, c(‘income’,‘score’)],centers = NGROUPS, nstart = 25)

c <- vector(‘list’, NGROUPS)

matplot(x = d\(income, y = d\)score, pch = 16, type = ‘p’, col = COL[1:NGROUPS] ) grid() for (i in 1:NGROUPS) { c[[i]] <- d[km$cluster == i, ]

matpoints(x = c[[i]]\(income, y = c[[i]]\)score, pch = 16, col = COL[i]) }

legend(‘topright’, pch = 16, bg=‘white’, col = COL[1:NGROUPS], legend = paste0(‘Group’,1:NGROUPS))