0

I'm new to R and I'm trying to do a PAM clustering analysis and visualize the results via t-SNE. However, I keep getting this error Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "factor". The problem is I'm working with chr type of values as well as numeric. I've been trying to do the cluster analysis without getting rid of the chr and so I used the PAM method listed here: https://www.r-bloggers.com/2016/06/clustering-mixed-data-types-in-r/

This is my data:

# A tibble: 6 × 5
  country   health_expen gdp_pc life_exp healthsoc_empl
  <chr>            <dbl>  <dbl>    <dbl>          <dbl>
1 Australia        5130. 52669.     82.9           68  
2 Austria          5624. 58076.     82             53.0
3 Belgium          5353. 54278.     82.1           56.4
4 Canada           5190. 49299.     82.3           59.9
5 Chile            2297. 25189.     80.6           26.5
6 Colombia         1293. 15662.     76.6           18.4

and this is what I've been trying to do, where 'df2' is my data:

#cluster interpretation
pam_fit <- pam(gower_dist, diss = TRUE, k = 2)
pam_results <- df2 %>%
dplyr::select(-country) %>%
mutate(cluster = pam_fit$clustering) %>%
group_by(cluster) %>%
do(the_summary = summary(.))
pam_results$the_summary

df2[pam_fit$medoids, ]
#visualize variables with t-distributted stochastic neigbourhood embedding
tsne_obj<- Rtsne(gower_dist, is_distance = TRUE,dims = 3, perplexity = 1)
tsne_data <- tsne_obj$Y %>%
data.frame() %>%
setNames(c("X", "Y")) %>%
mutate(cluster = factor(pam_fit$clustering),name = df2$country)

ggplot(aes(x = X, y = Y), data = tsne_data) +
geom_point(aes(color = cluster))

But as I said, I get an error because of the object of class "factor". What should I do?

  • Do you have multiple points per country or is it just one point per country? – G5W Nov 11 '22 at 22:50
  • 1
    What happens if you run all the code block togheter? I suspect you selected a line and accidentally run it. – Ric Nov 11 '22 at 22:56

0 Answers0