data <- read_excel("../00_data/Valentine'sDayHistoricalSpending.xlsx")
Case of numeric variables
data %>% map_dbl(.x = ., .f = ~mean(x = .x))
## Year PercentCelebrating PerPerson Candy
## 2016.000000 55.461538 144.449231 12.837692
## Flowers Jewelry GreetingCards EveningOut
## 14.653077 32.546154 7.676154 27.467692
## Clothing GiftCards
## 14.935385 11.503077
data %>% map_dbl(.f = ~mean(x = .x))
## Year PercentCelebrating PerPerson Candy
## 2016.000000 55.461538 144.449231 12.837692
## Flowers Jewelry GreetingCards EveningOut
## 14.653077 32.546154 7.676154 27.467692
## Clothing GiftCards
## 14.935385 11.503077
# Adding an argument
data %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.1))
## Year PercentCelebrating PerPerson Candy
## 2016.000000 55.454545 143.502727 12.817273
## Flowers Jewelry GreetingCards EveningOut
## 14.677273 32.348182 7.715455 27.475455
## Clothing GiftCards
## 14.752727 11.263636
data %>% map_dbl(mean, trim = 0.1)
## Year PercentCelebrating PerPerson Candy
## 2016.000000 55.454545 143.502727 12.817273
## Flowers Jewelry GreetingCards EveningOut
## 14.677273 32.348182 7.715455 27.475455
## Clothing GiftCards
## 14.752727 11.263636
data %>% select(.data = ., Flowers)
## # A tibble: 13 × 1
## Flowers
## <dbl>
## 1 12.3
## 2 12.6
## 3 13.5
## 4 13.5
## 5 15
## 6 15.7
## 7 14.8
## 8 14.6
## 9 14.8
## 10 15.1
## 11 16.5
## 12 15.4
## 13 16.7
data %>% select(Flowers)
## # A tibble: 13 × 1
## Flowers
## <dbl>
## 1 12.3
## 2 12.6
## 3 13.5
## 4 13.5
## 5 15
## 6 15.7
## 7 14.8
## 8 14.6
## 9 14.8
## 10 15.1
## 11 16.5
## 12 15.4
## 13 16.7
Create your own function
# Double values in columns
double_by_factor <- function(x, factor) {x * factor}
10 %>% double_by_factor(factor = 2)
## [1] 20
data %>% map(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## $Year
## [1] 20100 20110 20120 20130 20140 20150 20160 20170 20180 20190 20200 20210
## [13] 20220
##
## $PercentCelebrating
## [1] 600 580 590 600 540 550 550 540 550 510 550 520 530
##
## $PerPerson
## [1] 1030.0 1162.1 1260.3 1309.7 1339.1 1423.1 1468.4 1365.7 1435.6 1619.6
## [11] 1963.1 1647.6 1754.1
##
## $Candy
## [1] 86.0 107.5 108.5 116.4 108.0 127.0 131.1 126.8 131.2 141.2 173.0 153.2
## [13] 159.0
##
## $Flowers
## [1] 123.3 126.2 134.9 134.8 150.0 157.2 147.8 146.3 147.5 150.7 164.9 154.2
## [13] 167.1
##
## $Jewelry
## [1] 215.2 261.8 296.0 309.4 305.8 363.0 331.1 323.2 341.0 303.4 416.5 307.1
## [13] 457.5
##
## $GreetingCards
## [1] 59.1 80.9 69.3 83.2 79.7 78.7 85.2 73.6 65.5 73.1 90.1 84.8 74.7
##
## $EveningOut
## [1] 237.6 248.6 256.6 279.3 274.8 272.7 334.6 284.6 269.6 277.2 307.8 213.9
## [13] 313.5
##
## $Clothing
## [1] 109.3 120.0 104.2 114.6 133.7 147.2 150.5 139.1 140.4 160.8 206.7 200.5
## [13] 214.6
##
## $GiftCards
## [1] 84.2 112.1 84.3 102.3 90.0 110.5 125.2 102.3 110.4 103.1 142.1 156.7
## [13] 172.2
data %>% map_dfr(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## # A tibble: 13 × 10
## Year PercentCelebrating PerPerson Candy Flowers Jewelry GreetingCards
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 20100 600 1030 86 123. 215. 59.1
## 2 20110 580 1162. 108. 126. 262. 80.9
## 3 20120 590 1260. 108. 135. 296 69.3
## 4 20130 600 1310. 116. 135. 309. 83.2
## 5 20140 540 1339. 108 150 306. 79.7
## 6 20150 550 1423. 127 157. 363 78.7
## 7 20160 550 1468. 131. 148. 331. 85.2
## 8 20170 540 1366. 127. 146. 323. 73.6
## 9 20180 550 1436. 131. 148. 341 65.5
## 10 20190 510 1620. 141. 151. 303. 73.1
## 11 20200 550 1963. 173 165. 416. 90.1
## 12 20210 520 1648. 153. 154. 307. 84.8
## 13 20220 530 1754. 159 167. 458. 74.7
## # ℹ 3 more variables: EveningOut <dbl>, Clothing <dbl>, GiftCards <dbl>
When you have a grouping variable (factor)
data %>% lm(formula = Flowers ~ Jewelry, data = .)
##
## Call:
## lm(formula = Flowers ~ Jewelry, data = .)
##
## Coefficients:
## (Intercept) Jewelry
## 8.3520 0.1936
data %>% distinct(Candy)
## # A tibble: 13 × 1
## Candy
## <dbl>
## 1 8.6
## 2 10.8
## 3 10.8
## 4 11.6
## 5 10.8
## 6 12.7
## 7 13.1
## 8 12.7
## 9 13.1
## 10 14.1
## 11 17.3
## 12 15.3
## 13 15.9
reg_coeff_tbl <- data %>%
# Split it into a list of data frames
split(.$PercentCelebrating) %>%
# Repeat regression over each group
map(~lm(formula = Flowers ~ Jewelry, data = .x)) %>%
# Extract coefficients from regression results
map(broom::tidy, conf.int = TRUE) %>%
# Convert to tibble
bind_rows(.id = "PercentCelebrating") %>%
# Filter for weight coefficients
filter(term == "Jewelry")
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in qt(a, object$df.residual): NaNs produced
## Warning in qt(a, object$df.residual): NaNs produced
reg_coeff_tbl %>%
mutate(estimate = -estimate,
conf.low = -conf.low,
conf.high = -conf.high) %>%
ggplot(aes(x = estimate, y = PercentCelebrating)) +
geom_point() +
geom_errorbar(aes(xmin = conf.low, xmax = conf.high))
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).
Choose either one of the two cases above and apply it to your data
data %>% map_dbl(.x = ., .f = ~mean(x = .x))
## Year PercentCelebrating PerPerson Candy
## 2016.000000 55.461538 144.449231 12.837692
## Flowers Jewelry GreetingCards EveningOut
## 14.653077 32.546154 7.676154 27.467692
## Clothing GiftCards
## 14.935385 11.503077
data %>% map_dbl(.f = ~mean(x = .x))
## Year PercentCelebrating PerPerson Candy
## 2016.000000 55.461538 144.449231 12.837692
## Flowers Jewelry GreetingCards EveningOut
## 14.653077 32.546154 7.676154 27.467692
## Clothing GiftCards
## 14.935385 11.503077
# Adding an argument
data %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.1))
## Year PercentCelebrating PerPerson Candy
## 2016.000000 55.454545 143.502727 12.817273
## Flowers Jewelry GreetingCards EveningOut
## 14.677273 32.348182 7.715455 27.475455
## Clothing GiftCards
## 14.752727 11.263636
data %>% map_dbl(mean, trim = 0.1)
## Year PercentCelebrating PerPerson Candy
## 2016.000000 55.454545 143.502727 12.817273
## Flowers Jewelry GreetingCards EveningOut
## 14.677273 32.348182 7.715455 27.475455
## Clothing GiftCards
## 14.752727 11.263636
data %>% select(.data = ., Flowers)
## # A tibble: 13 × 1
## Flowers
## <dbl>
## 1 12.3
## 2 12.6
## 3 13.5
## 4 13.5
## 5 15
## 6 15.7
## 7 14.8
## 8 14.6
## 9 14.8
## 10 15.1
## 11 16.5
## 12 15.4
## 13 16.7
data %>% select(Flowers)
## # A tibble: 13 × 1
## Flowers
## <dbl>
## 1 12.3
## 2 12.6
## 3 13.5
## 4 13.5
## 5 15
## 6 15.7
## 7 14.8
## 8 14.6
## 9 14.8
## 10 15.1
## 11 16.5
## 12 15.4
## 13 16.7
# Double values in columns
double_by_factor <- function(x, factor) {x * factor}
10 %>% double_by_factor(factor = 2)
## [1] 20
data %>% map(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## $Year
## [1] 20100 20110 20120 20130 20140 20150 20160 20170 20180 20190 20200 20210
## [13] 20220
##
## $PercentCelebrating
## [1] 600 580 590 600 540 550 550 540 550 510 550 520 530
##
## $PerPerson
## [1] 1030.0 1162.1 1260.3 1309.7 1339.1 1423.1 1468.4 1365.7 1435.6 1619.6
## [11] 1963.1 1647.6 1754.1
##
## $Candy
## [1] 86.0 107.5 108.5 116.4 108.0 127.0 131.1 126.8 131.2 141.2 173.0 153.2
## [13] 159.0
##
## $Flowers
## [1] 123.3 126.2 134.9 134.8 150.0 157.2 147.8 146.3 147.5 150.7 164.9 154.2
## [13] 167.1
##
## $Jewelry
## [1] 215.2 261.8 296.0 309.4 305.8 363.0 331.1 323.2 341.0 303.4 416.5 307.1
## [13] 457.5
##
## $GreetingCards
## [1] 59.1 80.9 69.3 83.2 79.7 78.7 85.2 73.6 65.5 73.1 90.1 84.8 74.7
##
## $EveningOut
## [1] 237.6 248.6 256.6 279.3 274.8 272.7 334.6 284.6 269.6 277.2 307.8 213.9
## [13] 313.5
##
## $Clothing
## [1] 109.3 120.0 104.2 114.6 133.7 147.2 150.5 139.1 140.4 160.8 206.7 200.5
## [13] 214.6
##
## $GiftCards
## [1] 84.2 112.1 84.3 102.3 90.0 110.5 125.2 102.3 110.4 103.1 142.1 156.7
## [13] 172.2
data %>% map_dfr(.x = ., .f = ~double_by_factor(x = .x, factor = 10))
## # A tibble: 13 × 10
## Year PercentCelebrating PerPerson Candy Flowers Jewelry GreetingCards
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 20100 600 1030 86 123. 215. 59.1
## 2 20110 580 1162. 108. 126. 262. 80.9
## 3 20120 590 1260. 108. 135. 296 69.3
## 4 20130 600 1310. 116. 135. 309. 83.2
## 5 20140 540 1339. 108 150 306. 79.7
## 6 20150 550 1423. 127 157. 363 78.7
## 7 20160 550 1468. 131. 148. 331. 85.2
## 8 20170 540 1366. 127. 146. 323. 73.6
## 9 20180 550 1436. 131. 148. 341 65.5
## 10 20190 510 1620. 141. 151. 303. 73.1
## 11 20200 550 1963. 173 165. 416. 90.1
## 12 20210 520 1648. 153. 154. 307. 84.8
## 13 20220 530 1754. 159 167. 458. 74.7
## # ℹ 3 more variables: EveningOut <dbl>, Clothing <dbl>, GiftCards <dbl>