1
+ pacman :: p_load(tidyverse , glue , rvest )
2
+
3
+ # Make basketball_births first
4
+
5
+ # Make month_days tibble for wrangling later
6
+ month_days <- bind_rows(
7
+ tibble(month = 1 , days = 1 : 31 ),
8
+ tibble(month = 2 , days = 1 : 29 ),
9
+ tibble(month = 3 , days = 1 : 31 ),
10
+ tibble(month = 4 , days = 1 : 30 ),
11
+ tibble(month = 5 , days = 1 : 31 ),
12
+ tibble(month = 6 , days = 1 : 30 ),
13
+ tibble(month = 7 , days = 1 : 31 ),
14
+ tibble(month = 8 , days = 1 : 31 ),
15
+ tibble(month = 9 , days = 1 : 30 ),
16
+ tibble(month = 10 , days = 1 : 31 ),
17
+ tibble(month = 11 , days = 1 : 30 ),
18
+ tibble(month = 12 , days = 1 : 31 ),
19
+ )
20
+
21
+
22
+ basketball_date_rip <- function (m ,d ) {
23
+
24
+ urlpath <- glue(" https://www.basketball-reference.com/friv/birthdays.fcgi?month={month}&day={day}" ,
25
+ month = m , day = d )
26
+
27
+ out <- read_html(urlpath ) %> %
28
+ html_nodes(" table" ) %> %
29
+ html_table() %> %
30
+ . [[1 ]]
31
+
32
+ col_names <- str_remove_all(out [1 , 2 : 4 ], " \\ (s\\ )" ) %> % str_to_lower()
33
+
34
+ out <- out %> %
35
+ . [- 1 , 2 : 4 ]
36
+
37
+ colnames(out ) <- col_names
38
+
39
+ print(str_c(m ," /" , d ))
40
+
41
+ out %> %
42
+ mutate(birthday = ymd(str_c(born ," -" , m ," -" , d ))) %> %
43
+ select(player , birthday , everything(),- born )
44
+
45
+
46
+ }
47
+
48
+
49
+ basketball_list <- map2(month_days $ month , month_days $ days , ~ basketball_date_rip(.x , .y ))
50
+
51
+ basketball_births <- basketball_list %> %
52
+ bind_rows() %> %
53
+ as_tibble() %> %
54
+ mutate(yrs = as.integer(yrs ))
55
+
56
+ # Convert basketball_births into counts_basketball
57
+ counts_basketball <- basketball_births %> %
58
+ mutate(month = month(birthday ), day = mday(birthday )) %> %
59
+ count(month , day , name = " births" ) %> %
60
+ mutate(day_of_year = 1 : n()) %> %
61
+ left_join(tibble(month = 1 : 12 , month_name = month.name )) %> %
62
+ select(month_number = month , month_name , day_of_month = day , day_of_year , births )
63
+
64
+ # Data details
65
+ dpr_document(counts_basketball , extension = " .md.R" , export_folder = usethis :: proj_get(),
66
+ object_name = " counts_basketball" ,
67
+ title = " The count of births of NBA/ABA players" ,
68
+ description = " Data obtained from https://www.basketball-reference.com" ,
69
+ source = " https://www.basketball-reference.com/friv/birthdays.fcgi?month=1&day=1" ,
70
+ var_details = counts_description )
0 commit comments