0

This is a generic question related to functions:

Let's say I have the following function with random code within brackets. I found this code from a earlier thread from today: Add a column to function with fixed variable

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(2015)

Which generates 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>

I would like to change name of Team column to Club so it always has the name Club. I want to find a general code that works for column 2 in other functions aswell as there are functions where the data are same but column names differs (and I want one column name).

Something similar to below code that was brought as an previous answer is what I'm looking for:

    dat <- read.csv(url)
    names(dat)[2] <- "year"
    dat
  • 2
    Maybe `names(df)[2]<-"year"`? Hard to say without some sort of [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that actually shows you creating the data.frame. – MrFlick Jul 30 '22 at 20:12
  • This is as I mentioned just a generic question of something I have previously stumbled upon. However, the dataframe is created first after the function is completed. I guess the df needs to be mentioned in some other way? – Sara Dwindler Jul 30 '22 at 20:14
  • 1
    It's harder to answer generic questions missing important details that it is to help with a concrete example so we can run and test code to see what's going on. Since you showed `rename(df, Newname = Oldname)` I assumed you already had a variable named `df` somewhere. If that's not the case, please edit your question to show where the data frame is created. – MrFlick Jul 30 '22 at 20:17

2 Answers2

3

You can rename by index

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) %>% 
  rename(Club=2)
}
MrFlick
  • 195,160
  • 17
  • 277
  • 295
2

It can be done as

library(rvest)
library(dplyr)
read_prem_league <- function(year) { 
dat <- "https://en.wikipedia.org/wiki/" %>%
  paste0(year - 1, "-", substr(as.character(year), 3, 4), "_Premier_League") %>%
  read_html() %>% 
  html_table() %>% 
  getElement(5)
names(dat)[2] <- "Club"
dat
}

-testing

> read_prem_league(2015)
# A tibble: 20 × 11
     Pos Club                      Pld     W     D     L    GF    GA GD      Pts `Qualification or relegation`                                  
   <int> <chr>                   <int> <int> <int> <int> <int> <int> <chr> <int> <chr>                                                          
 1     1 Chelsea (C)                38    26     9     3    73    32 +41      87 "Qualification for the Champions League group stage"           
 2     2 Manchester City            38    24     7     7    83    38 +45      79 "Qualification for the Champions League group stage"           
 3     3 Arsenal                    38    22     9     7    71    36 +35      75 "Qualification for the Champions League group stage"           
 4     4 Manchester United          38    20    10     8    62    37 +25      70 "Qualification for the Champions League play-off round"        
 5     5 Tottenham Hotspur          38    19     7    12    58    53 +5       64 "Qualification for the Europa League group stage[a]"           
 6     6 Liverpool                  38    18     8    12    52    48 +4       62 "Qualification for the Europa League group stage[a]"           
 7     7 Southampton                38    18     6    14    54    33 +21      60 "Qualification for the Europa League third qualifying round[a]"
 8     8 Swansea City               38    16     8    14    46    49 −3       56 ""                                                             
 9     9 Stoke City                 38    15     9    14    48    45 +3       54 ""                                                             
10    10 Crystal Palace             38    13     9    16    47    51 −4       48 ""                                                             
11    11 Everton                    38    12    11    15    48    50 −2       47 ""                                                             
12    12 West Ham United            38    12    11    15    44    47 −3       47 "Qualification for the Europa League first qualifying round[b]"
13    13 West Bromwich Albion       38    11    11    16    38    51 −13      44 ""                                                             
14    14 Leicester City             38    11     8    19    46    55 −9       41 ""                                                             
15    15 Newcastle United           38    10     9    19    40    63 −23      39 ""                                                             
16    16 Sunderland                 38     7    17    14    31    53 −22      38 ""                                                             
17    17 Aston Villa                38    10     8    20    31    57 −26      38 ""                                                             
18    18 Hull City (R)              38     8    11    19    33    51 −18      35 "Relegation to the Football League Championship"               
19    19 Burnley (R)                38     7    12    19    28    53 −25      33 "Relegation to the Football League Championship"               
20    20 Queens Park Rangers (R)    38     8     6    24    42    73 −31      30 "Relegation to the Football League Championship"               
akrun
  • 874,273
  • 37
  • 540
  • 662