##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (ic.load,cut=0,labels=NULL,loading=FALSE) {
if (length(labels)==0) {
var.labels <- rownames(ic.load)} else {var.labels=labels}
if(loading) {loadings <- ic.load$loadings} else {loadings <- ic.load}
nclust <- dim(loadings)[2]
nitems <- dim(loadings)[1]
load <- data.frame(item=seq(1:nitems),content=var.labels,cluster=rep(0,nitems),loadings)
#first find the maximum for each row and assign it to that cluster
load$cluster <- apply(abs(loadings),1,which.max)
for (i in 1:nitems) {if (abs(loadings[i,load$cluster[i]]) < cut) {load$cluster[i] <-nclust+1}}
ord <- sort(load$cluster,index.return=TRUE)
load[1:nitems,] <- load[ord$ix,]
rownames(load)[1:nitems] <- rownames(load)[ord$ix]
items <- c(table(load$cluster),1) #how many items are in each cluster?
#now sort the loadings that have their highest loading on each cluster
first <- 1
for (i in 1:nclust) {
last <- first + items[i]- 1
ord <- sort(abs(load[first:last,i+3]),decreasing=TRUE,index.return=TRUE)
load[first:last,] <- load[ord$ix+first-1,]
rownames(load)[first:last] <- rownames(load)[ord$ix+first-1]
first <- first + items[i]
}
if (first < nitems) load[first:nitems,"cluster"] <- 0 #assign items less than cut to 0
ICLUST.sort <- load }
Run the code above in your browser using DataLab