data("mtcars")
mtcars <- as_tibble(mtcars)
mydata <- read_xlsx("../00_data/data/myData.xlsx")
mydata
## # A tibble: 500 × 21
## sort_name clean_name album rank_2003 rank_2012 rank_2020 differential
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Beatles The Beatles Sgt.… 1 1 24 -23
## 2 Beach Boys The Beach Bo… Pet … 2 2 2 0
## 3 Beatles The Beatles Revo… 3 3 11 -8
## 4 Dylan, Bob Bob Dylan High… 4 4 18 -14
## 5 Beatles The Beatles Rubb… 5 5 35 -30
## 6 Gaye, Marvin Marvin Gaye What… 6 6 1 5
## 7 Rolling Stones Rolling Ston… Exil… 7 7 14 -7
## 8 Clash The Clash Lond… 8 8 16 -8
## 9 Dylan, Bob Bob Dylan Blon… 9 9 38 -29
## 10 Beatles The Beatles The … 10 10 29 -19
## # ℹ 490 more rows
## # ℹ 14 more variables: release_year <dbl>, genre <chr>, type <chr>,
## # weeks_on_billboard <dbl>, peak_billboard_position <dbl>,
## # spotify_popularity <dbl>, spotify_url <chr>, artist_member_count <dbl>,
## # artist_gender <chr>, artist_birth_year_sum <dbl>,
## # debut_album_release_year <dbl>, ave_age_at_top_500 <dbl>,
## # years_between <dbl>, album_id <chr>
Case of numeric variables
mtcars %>% map_dbl(.x = ., .f = ~mean(x = .x))
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
mtcars %>% map_dbl(.f = ~mean(x = .x))
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
mtcars %>% map_dbl(mean)
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
# Adding an Argument
mtcars %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.1))
## mpg cyl disp hp drat wt
## 19.6961538 6.2307692 222.5230769 141.1923077 3.5792308 3.1526923
## qsec vs am gear carb
## 17.8276923 0.4230769 0.3846154 3.6153846 2.6538462
mtcars %>% map_dbl(mean, trim = 0.1)
## mpg cyl disp hp drat wt
## 19.6961538 6.2307692 222.5230769 141.1923077 3.5792308 3.1526923
## qsec vs am gear carb
## 17.8276923 0.4230769 0.3846154 3.6153846 2.6538462
mtcars %>% select(.data = .,mpg)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # ℹ 22 more rows
mtcars %>% select(mpg)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # ℹ 22 more rows
Create your own function
#Double Values in Columns
double_by_factor <- function(x, factor){x * factor}
10 %>% double_by_factor(factor = 2)
## [1] 20
mtcars %>% map_dfr(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## # A tibble: 32 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 210 60 1600 1100 39 26.2 165. 0 10 40 40
## 2 210 60 1600 1100 39 28.8 170. 0 10 40 40
## 3 228 40 1080 930 38.5 23.2 186. 10 10 40 10
## 4 214 60 2580 1100 30.8 32.2 194. 10 0 30 10
## 5 187 80 3600 1750 31.5 34.4 170. 0 0 30 20
## 6 181 60 2250 1050 27.6 34.6 202. 10 0 30 10
## 7 143 80 3600 2450 32.1 35.7 158. 0 0 30 40
## 8 244 40 1467 620 36.9 31.9 200 10 0 40 20
## 9 228 40 1408 950 39.2 31.5 229 10 0 40 20
## 10 192 60 1676 1230 39.2 34.4 183 10 0 40 40
## # ℹ 22 more rows
mtcars %>% map_dfr(double_by_factor, factor = 10)
## # A tibble: 32 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 210 60 1600 1100 39 26.2 165. 0 10 40 40
## 2 210 60 1600 1100 39 28.8 170. 0 10 40 40
## 3 228 40 1080 930 38.5 23.2 186. 10 10 40 10
## 4 214 60 2580 1100 30.8 32.2 194. 10 0 30 10
## 5 187 80 3600 1750 31.5 34.4 170. 0 0 30 20
## 6 181 60 2250 1050 27.6 34.6 202. 10 0 30 10
## 7 143 80 3600 2450 32.1 35.7 158. 0 0 30 40
## 8 244 40 1467 620 36.9 31.9 200 10 0 40 20
## 9 228 40 1408 950 39.2 31.5 229 10 0 40 20
## 10 192 60 1676 1230 39.2 34.4 183 10 0 40 40
## # ℹ 22 more rows
When you have a grouping variable (factor)
mtcars %>% lm(formula = mpg ~ wt, data = .)
##
## Call:
## lm(formula = mpg ~ wt, data = .)
##
## Coefficients:
## (Intercept) wt
## 37.285 -5.344
mtcars %>% distinct(cyl)
## # A tibble: 3 × 1
## cyl
## <dbl>
## 1 6
## 2 4
## 3 8
reg_coeff_tbl <- mtcars %>%
#Split into list of data frames
split(.$cyl) %>%
#Repeat regression over each group
map(~lm(formula = mpg ~ wt, data = .x)) %>%
#Extract Coefficient from regression results
map(broom::tidy, conf.int = TRUE) %>%
#Convert to Tibble
bind_rows(.id = "cyl") %>%
#Filter for wt coefficients
filter(term == "wt")
reg_coeff_tbl %>%
mutate(estimate = -estimate,
conf.low = -conf.low,
conf.high = -conf.high) %>%
ggplot(aes(x = estimate, y = cyl)) +
geom_point() +
geom_errorbar(aes(xmin = conf.low, xmax = conf.high))
Choose either one of the two cases above and apply it to your data
# Collapse genres into broader categories and count them
collapsed_data <- mydata %>%
mutate(genre = fct_collapse(genre,
`Rock & Alternative` = c("Blues/Blues Rock",
"Blues/Blues ROck",
"Hard Rock/Metal",
"Indie/Alternative Rock",
"Punk/Post-Punk/New Wave/Power Pop",
"Rock n' Roll/Rhythm & Blues"),
`Pop & Soul` = c("Funk/Disco",
"Soul/Gospel/R&B",
"Singer-Songwriter/Heartland Rock",
"Big Band/Jazz"),
`Roots & Folk` = c("Country/Folk/Country Rock/Folk Rock",
"Blues/Blues Rock",
"Singer-Songwriter/Heartland Rock"),
`Global Rhythms` = c("Afrobeat",
"Latin",
"Reggae"),
`Hip-Hop & Electronic` = c("Hip-Hop/Rap",
"Electronic")))
# Count rows in each collapsed genre (optional, for review)
collapsed_data %>%
count(genre) %>%
arrange(desc(n))
## # A tibble: 6 × 2
## genre n
## <fct> <int>
## 1 Rock & Alternative 132
## 2 <NA> 110
## 3 Pop & Soul 94
## 4 Roots & Folk 83
## 5 Hip-Hop & Electronic 68
## 6 Global Rhythms 13
# Perform regression analysis on collapsed genres
reg_coeff_tbl <- collapsed_data %>%
# Split into a list of data frames by collapsed genre
split(.$genre) %>%
# Perform regression for each genre group
map(~lm(formula = spotify_popularity ~ rank_2020, data = .x)) %>%
# Extract coefficients and confidence intervals
map(broom::tidy, conf.int = TRUE) %>%
# Combine results into a single tibble
bind_rows(.id = "genre") %>%
# Filter for coefficients of interest (rank_2020)
filter(term == "rank_2020")
# Adjust signs and visualize results
reg_coeff_tbl %>%
mutate(estimate = -estimate,
conf.low = -conf.low,
conf.high = -conf.high) %>%
ggplot(aes(x = estimate, y = genre)) +
geom_point() +
geom_errorbar(aes(xmin = conf.low, xmax = conf.high)) +
labs(title = "Effect of Rank on Spotify Popularity by Genre Group",
x = "Regression Coefficient (Reversed)",
y = "Genre Group")