data("mtcars")
mtcars <- as_tibble(mtcars)
rating <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2022/2022-01-25/ratings.csv', show_col_types = FALSE)
rating
## # A tibble: 21,831 × 10
## num id name year rank average bayes_average users_rated url
## <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
## 1 105 30549 Pandemic 2008 106 7.59 7.49 108975 /boa…
## 2 189 822 Carcassonne 2000 190 7.42 7.31 108738 /boa…
## 3 428 13 Catan 1995 429 7.14 6.97 108024 /boa…
## 4 72 68448 7 Wonders 2010 73 7.74 7.63 89982 /boa…
## 5 103 36218 Dominion 2008 104 7.61 7.50 81561 /boa…
## 6 191 9209 Ticket to R… 2004 192 7.41 7.30 76171 /boa…
## 7 100 178900 Codenames 2015 101 7.6 7.51 74419 /boa…
## 8 3 167791 Terraformin… 2016 4 8.42 8.27 74216 /boa…
## 9 15 173346 7 Wonders D… 2015 16 8.11 7.98 69472 /boa…
## 10 35 31260 Agricola 2007 36 7.93 7.81 66093 /boa…
## # ℹ 21,821 more rows
## # ℹ 1 more variable: thumbnail <chr>
ratings <- head(rating, 50)
ratings
## # A tibble: 50 × 10
## num id name year rank average bayes_average users_rated url
## <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
## 1 105 30549 Pandemic 2008 106 7.59 7.49 108975 /boa…
## 2 189 822 Carcassonne 2000 190 7.42 7.31 108738 /boa…
## 3 428 13 Catan 1995 429 7.14 6.97 108024 /boa…
## 4 72 68448 7 Wonders 2010 73 7.74 7.63 89982 /boa…
## 5 103 36218 Dominion 2008 104 7.61 7.50 81561 /boa…
## 6 191 9209 Ticket to R… 2004 192 7.41 7.30 76171 /boa…
## 7 100 178900 Codenames 2015 101 7.6 7.51 74419 /boa…
## 8 3 167791 Terraformin… 2016 4 8.42 8.27 74216 /boa…
## 9 15 173346 7 Wonders D… 2015 16 8.11 7.98 69472 /boa…
## 10 35 31260 Agricola 2007 36 7.93 7.81 66093 /boa…
## # ℹ 40 more rows
## # ℹ 1 more variable: thumbnail <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
get_fib <- function(n) {
if(n == 0) 1
else if(n == 1) 1
else get_fib(n-1)+get_fib(n-2)
}
get_fib(5)
## [1] 8
mtcars$mpg %>% map(~get_fib(as.integer(.x)))
## [[1]]
## [1] 17711
##
## [[2]]
## [1] 17711
##
## [[3]]
## [1] 28657
##
## [[4]]
## [1] 17711
##
## [[5]]
## [1] 4181
##
## [[6]]
## [1] 4181
##
## [[7]]
## [1] 610
##
## [[8]]
## [1] 75025
##
## [[9]]
## [1] 28657
##
## [[10]]
## [1] 6765
##
## [[11]]
## [1] 2584
##
## [[12]]
## [1] 1597
##
## [[13]]
## [1] 2584
##
## [[14]]
## [1] 987
##
## [[15]]
## [1] 89
##
## [[16]]
## [1] 89
##
## [[17]]
## [1] 610
##
## [[18]]
## [1] 3524578
##
## [[19]]
## [1] 1346269
##
## [[20]]
## [1] 5702887
##
## [[21]]
## [1] 17711
##
## [[22]]
## [1] 987
##
## [[23]]
## [1] 987
##
## [[24]]
## [1] 377
##
## [[25]]
## [1] 6765
##
## [[26]]
## [1] 317811
##
## [[27]]
## [1] 196418
##
## [[28]]
## [1] 1346269
##
## [[29]]
## [1] 987
##
## [[30]]
## [1] 6765
##
## [[31]]
## [1] 987
##
## [[32]]
## [1] 17711
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 a list of data frames
split(.$cyl) %>%
# Repeat regression over each frame
map(~lm(formula = mpg ~ wt, data = .x)) %>%
# Extract coefficients 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
ratings$average %>% map(~get_fib(as.integer(.x)))
## [[1]]
## [1] 21
##
## [[2]]
## [1] 21
##
## [[3]]
## [1] 21
##
## [[4]]
## [1] 21
##
## [[5]]
## [1] 21
##
## [[6]]
## [1] 21
##
## [[7]]
## [1] 21
##
## [[8]]
## [1] 34
##
## [[9]]
## [1] 34
##
## [[10]]
## [1] 21
##
## [[11]]
## [1] 21
##
## [[12]]
## [1] 21
##
## [[13]]
## [1] 34
##
## [[14]]
## [1] 21
##
## [[15]]
## [1] 21
##
## [[16]]
## [1] 21
##
## [[17]]
## [1] 21
##
## [[18]]
## [1] 21
##
## [[19]]
## [1] 21
##
## [[20]]
## [1] 34
##
## [[21]]
## [1] 21
##
## [[22]]
## [1] 21
##
## [[23]]
## [1] 34
##
## [[24]]
## [1] 21
##
## [[25]]
## [1] 21
##
## [[26]]
## [1] 21
##
## [[27]]
## [1] 34
##
## [[28]]
## [1] 21
##
## [[29]]
## [1] 34
##
## [[30]]
## [1] 13
##
## [[31]]
## [1] 8
##
## [[32]]
## [1] 34
##
## [[33]]
## [1] 34
##
## [[34]]
## [1] 21
##
## [[35]]
## [1] 21
##
## [[36]]
## [1] 21
##
## [[37]]
## [1] 21
##
## [[38]]
## [1] 21
##
## [[39]]
## [1] 21
##
## [[40]]
## [1] 21
##
## [[41]]
## [1] 21
##
## [[42]]
## [1] 21
##
## [[43]]
## [1] 21
##
## [[44]]
## [1] 21
##
## [[45]]
## [1] 21
##
## [[46]]
## [1] 21
##
## [[47]]
## [1] 21
##
## [[48]]
## [1] 21
##
## [[49]]
## [1] 21
##
## [[50]]
## [1] 34