jeudi 8 juillet 2021

for loop, function, if statement in r

I have a code below, lets say 1) I want to implement clustering (where the cluster numbers are k = 2, 3, 5) in a dataset and save the results as y1 and y2. 2) change the column name in Cluster.No 3) see the summary, head, or the tail of each clustered data set. Many thanks in advance.

library(cluster)
data(iris); head(iris)
iris2 <- iris[,-5]

my.functions <- function(x){
  
  for (k in 2:5) {
    y1 <- y2 <- km.out <- NULL
    
    set.seed(87)
    km.out[[k]] <- kmeans(iris2, centers = k, nstart = 20, iter.max = 50)
    
    df.clstr[[k]] <- cbind(km.out[[k]]$cluster, iris2)
    names(df.clstr[[k]])[names(df.clstr[[k]]) == "km.out$cluster"] <- "Cluster.No"
    
    df.clstr$Cluster.No <- factor(df.clstr[[k]]$Cluster.No)
    
    y1[[k]] <- km.out$size
    y2[[k]] <- aggregate(iris2, by=list(df.clstr$Cluster.No), FUN=mean)
    
  }
  
  result1 <- result2 <- NULL
  
  
  if(x == "sum"){
    
    result1[[k]] <- summary(df.clstr)
    
    output <- list(result1 = result1)
    return(output)
    
    
    
  }else if(x == "head"){
    
    result2[[k]] <- head(df.clstr)
    
    output <- list(result2 = result2)
    return(output)
    
    
  }else(x == "tail")
  
  result3[[k]] <- tail(df.clstr)
  
  output <- list(result3 = result3)
  return(output)
  
}

out1 <- my.functions("sum");out1
y1[[3]]

Aucun commentaire:

Enregistrer un commentaire