#' Clustering the respondents based on philosophy module using kmeans clustering method.
#' @param df
#'
#' @return
#' @export
#'
#' @examples
#' homework::kmeans_cluster(survey_data)
kmeans_cluster <- function(df)
{
age <- df %>% dplyr::select(response_id,s_age)
df <- df%>%
dplyr::select(response_id , contains('m1_philosophy'))
dm <- data.matrix(df[2:10])
km <- kmeans(dm, 4 , nstart = 50)
joined <- cbind(df , cluster = km$cluster) %>%
dplyr::left_join(age) %>%
dplyr::mutate(cluster = paste0("Cluster ", cluster))%>%
dplyr::select( response_id , cluster,s_age)
Segment <- joined %>%
dplyr::group_by(cluster , s_age) %>%
dplyr::summarise(count = dplyr::n(), .groups = 'drop' ) %>%
dplyr::ungroup() %>%
tidyr::pivot_wider(names_from = cluster , values_from = count)
return(Segment)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.