Skip to content

Instantly share code, notes, and snippets.

@tts
Last active September 29, 2021 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tts/9c953059d4686c634d5a2cf287080cf8 to your computer and use it in GitHub Desktop.
Save tts/9c953059d4686c634d5a2cf287080cf8 to your computer and use it in GitHub Desktop.
Mapping some toy music subgenres
# https://raw.githubusercontent.com/gkaramanis/tidytuesday/master/2021/2021-week37/billboard.R
# https://twitter.com/ttso/status/1442485258172043268
billb_feat <- billb_feat %>%
mutate(
subgenre = case_when(
str_detect(spotify_genre, "rock") ~ "1",
str_detect(spotify_genre, "folk") ~ "2",
str_detect(spotify_genre, "pop") ~ "3",
str_detect(spotify_genre, "jazz") ~ "4",
str_detect(spotify_genre, "soul") ~ "5",
str_detect(spotify_genre, "blues") ~ "6",
TRUE ~ "0"
)
)
ggplot(billb_feat, aes(x = year, y = n, fill = subgenre)) + ...
# Reordering factor levels
# https://twitter.com/geokaramanis/status/1442840086568112132
billb_feat <- billb_feat %>%
ungroup() %>%
# some fake subgenres
mutate(
subgenre = case_when(
str_detect(spotify_genre, "rock") ~ "1",
str_detect(spotify_genre, "folk") ~ "2",
str_detect(spotify_genre, "pop") ~ "3",
str_detect(spotify_genre, "jazz") ~ "4",
str_detect(spotify_genre, "soul") ~ "5",
str_detect(spotify_genre, "blues") ~ "6",
TRUE ~ "0"
),
spotify_genre = forcats::fct_reorder(factor(spotify_genre), factor(subgenre))
)
# Problem with `mutate()` column `spotify_genre`.
# i `spotify_genre = forcats::fct_reorder(factor(spotify_genre), factor(subgenre))`.
# x need numeric data
# Backtrace:
# x
# 1. +-`%>%`(...)
# 2. +-dplyr::mutate(...)
# 3. +-dplyr:::mutate.data.frame(...)
# 4. | \-dplyr:::mutate_cols(.data, ..., caller_env = caller_env())
# 5. | +-base::withCallingHandlers(...)
# 6. | \-mask$eval_all_mutate(quo)
# 7. +-forcats::fct_reorder(factor(spotify_genre), factor(subgenre))
# 8. | \-base::tapply(.x, .f, .fun, ...)
# 9. | \-base::lapply(X = ans[index], FUN = FUN, ...)
# 10. | +-stats:::FUN(X[[i]], ...)
# 11. | \-stats::median.default(X[[i]], ...)
# 12. | \-base::stop("need numeric data")
# 13. \-base::.handleSimpleError(...)
# 14. \-dplyr:::h(simpleError(msg, call))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment