I have this code as a function which generates the table of a Premier League season from Wiki.
read_prem_league <- function(year) {
"https://en.wikipedia.org/wiki/" %>%
paste0(year - 1, "-", substr(as.character(year), 3, 4), "_Premier_League") %>%
read_html() %>%
html_table() %>%
getElement(5)
}
read_prem_league(2021)
Who would create the following tibble:
#> # A tibble: 20 x 11
#> Pos Team Pld W D L GF GA GD Pts
#> <int> <chr> <int> <int> <int> <int> <int> <int> <chr> <int>
#> 1 1 Manchester City (C) 38 27 5 6 83 32 51 86
#> 2 2 Manchester United 38 21 11 6 73 44 29 74
#> 3 3 Liverpool 38 20 9 9 68 42 26 69
#> 4 4 Chelsea 38 19 10 9 58 36 22 67
#> 5 5 Leicester City 38 20 6 12 68 50 18 66
#> 6 6 West Ham United 38 19 8 11 62 47 15 65
#> 7 7 Tottenham Hotspur 38 18 8 12 68 45 23 62
#> 8 8 Arsenal 38 18 7 13 55 39 16 61
#> 9 9 Leeds United 38 18 5 15 62 54 8 59
#> 10 10 Everton 38 17 8 13 47 48 -1 59
#> 11 11 Aston Villa 38 16 7 15 55 46 9 55
#> 12 12 Newcastle United 38 12 9 17 46 62 -16 45
#> 13 13 Wolverhampton Wande~ 38 12 9 17 36 52 -16 45
#> 14 14 Crystal Palace 38 12 8 18 41 66 -25 44
#> 15 15 Southampton 38 12 7 19 47 68 -21 43
#> 16 16 Brighton & Hove Alb~ 38 9 14 15 40 46 -6 41
#> 17 17 Burnley 38 10 9 19 33 55 -22 39
#> 18 18 Fulham (R) 38 5 13 20 27 53 -26 28
#> 19 19 West Bromwich Albio~ 38 5 11 22 35 76 -41 26
#> 20 20 Sheffield United (R) 38 7 2 29 20 63 -43 23
#> # ... with 1 more variable: `Qualification or relegation` <chr>
What I would like to do is to add a column called Season
to the left of Pos
which shows the current season, so it it's the season ending in 2020 I want it to to say 2019-20.
read_prem_league$Season <- (year)
The above code should work and I want to put it within the function. However, I get the error: Error in View : object of type 'closure' is not subsettable
CodePudding user response:
We may use mutate
library(dplyr)
library(rvest)
read_prem_league <- function(year) {
"https://en.wikipedia.org/wiki/" %>%
paste0(year - 1, "-", substr(as.character(year), 3, 4),
"_Premier_League") %>%
read_html() %>%
html_table() %>%
getElement(5) %>%
dplyr::mutate(Season = year, .before = Pos)
}
-testing
> dat <- read_prem_league(2021)
> dat
# A tibble: 20 × 12
Season Pos Team Pld W D L GF GA GD Pts `Qualification or relegation`
<dbl> <int> <chr> <int> <int> <int> <int> <int> <int> <chr> <int> <chr>
1 2021 1 Manchester City (C) 38 27 5 6 83 32 51 86 "Qualification for the Champions League group stage"
2 2021 2 Manchester United 38 21 11 6 73 44 29 74 "Qualification for the Champions League group stage"
3 2021 3 Liverpool 38 20 9 9 68 42 26 69 "Qualification for the Champions League group stage"
4 2021 4 Chelsea 38 19 10 9 58 36 22 67 "Qualification for the Champions League group stage"
5 2021 5 Leicester City 38 20 6 12 68 50 18 66 "Qualification for the Europa League group stage[a]"
6 2021 6 West Ham United 38 19 8 11 62 47 15 65 "Qualification for the Europa League group stage[a]"
7 2021 7 Tottenham Hotspur 38 18 8 12 68 45 23 62 "Qualification for the Europa Conference League play-off round[b]"
8 2021 8 Arsenal 38 18 7 13 55 39 16 61 ""
9 2021 9 Leeds United 38 18 5 15 62 54 8 59 ""
10 2021 10 Everton 38 17 8 13 47 48 −1 59 ""
11 2021 11 Aston Villa 38 16 7 15 55 46 9 55 ""
12 2021 12 Newcastle United 38 12 9 17 46 62 −16 45 ""
13 2021 13 Wolverhampton Wanderers 38 12 9 17 36 52 −16 45 ""
14 2021 14 Crystal Palace 38 12 8 18 41 66 −25 44 ""
15 2021 15 Southampton 38 12 7 19 47 68 −21 43 ""
16 2021 16 Brighton & Hove Albion 38 9 14 15 40 46 −6 41 ""
17 2021 17 Burnley 38 10 9 19 33 55 −22 39 ""
18 2021 18 Fulham (R) 38 5 13 20 27 53 −26 28 "Relegation to the EFL Championship"
19 2021 19 West Bromwich Albion (R) 38 5 11 22 35 76 −41 26 "Relegation to the EFL Championship"
20 2021 20 Sheffield United (R) 38 7 2 29 20 63 −43 23 "Relegation to the EFL Championship"