user8248672
user8248672

Reputation:

Creating a function to supply parameters to another function that exists

Right now, I have a main function (let's call it performance()) that has as its arguments player1, player2, and team_of_interest.

I have a data set that looks like this:

 > head(roster_van, 3)
          team_name team venue        num_first_last
1 VANCOUVER CANUCKS  VAN  Home         5 SBISA, LUCA
2 VANCOUVER CANUCKS  VAN  Home  8 TANEV, CHRISTOPHER
3 VANCOUVER CANUCKS  VAN  Home 14 BURROWS, ALEXANDRE
   game_date    game_id   season session player_number
1 2016-10-15 2016020029 20162017       R             5
2 2016-10-15 2016020029 20162017       R             8
3 2016-10-15 2016020029 20162017       R            14
  team_num  first_name last_name  player_name
1     VAN5        LUCA     SBISA   LUCA.SBISA
2     VAN8 CHRISTOPHER     TANEV  CHRIS.TANEV
3    VAN14   ALEXANDRE   BURROWS ALEX.BURROWS
        name_match player_position
1        LUCASBISA               D
2 CHRISTOPHERTANEV               D
3 ALEXANDREBURROWS               L

This is the roster data for a hockey games played in a season.

I want to create another function (let's call it players()) that loops through every unique pair of players in a hockey team and provides their names and team to the player1, player2, and team_of_interest arguments inside the performance() function.

I've started off with this, but don't know what next to do:

name_pairs <- function(x,y) {
  x <- seq(1,19, by = 2)
  y <- x+1
}

Upvotes: 2

Views: 70

Answers (1)

leerssej
leerssej

Reputation: 14958

merge can make quick work of generating a cartesian join out of your dataframe.

With a shortened version of your sample dataframe and a guess at the team_of_interest column.

library(tidyverse)
roster_van <- tibble(team = "VAN",
                     team_num = c(5, 8, 14),
                     player_name = c("LUCA.SBISA", "CHRIS.TANEV", "ALEX.BURROWS"),
                     player_position = c("D", "D", "L"),
                     team_of_interest = c("SL BLUES", "BOS BRUINS", "CGY FLAMES")
                     )

roster_van
> roster_van
# A tibble: 3 x 5
   team team_num  player_name player_position team_of_interest
  <chr>    <dbl>        <chr>           <chr>            <chr>
1   VAN        5   LUCA.SBISA               D         SL BLUES
2   VAN        8  CHRIS.TANEV               D       BOS BRUINS
3   VAN       14 ALEX.BURROWS               L       CGY FLAMES

If you only want a few of the columns repeated, then only rename the column names you wish to see joined again onto the original dataframe before you filter off the equal self joins.

roster_van_pairs <- 
    roster_van %>%
    merge(roster_van %>%
              select(team,
                     team_num_paired = team_num,
                     player_name_paired = player_name
                     )
          ) %>% 
    filter(player_name != player_name_paired)
roster_van_pairs
> roster_van_pairs
  team team_num  player_name player_position team_of_interest team_num_paired player_name_paired
1  VAN        5   LUCA.SBISA               D         SL BLUES               8        CHRIS.TANEV
2  VAN        5   LUCA.SBISA               D         SL BLUES              14       ALEX.BURROWS
3  VAN        8  CHRIS.TANEV               D       BOS BRUINS               5         LUCA.SBISA
4  VAN        8  CHRIS.TANEV               D       BOS BRUINS              14       ALEX.BURROWS
5  VAN       14 ALEX.BURROWS               L       CGY FLAMES               5         LUCA.SBISA
6  VAN       14 ALEX.BURROWS               L       CGY FLAMES               8        CHRIS.TANEV

If you want to go with a bulk approach which will join all the columns in again, you can execute a full rename of all the columns with the code below:

roster_van_copy <- roster_van
# provenience the data quickly
colnames(roster_van_copy) <-  colnames(roster_van_copy) %>% paste0(., "_paired")

This makes the cross join code more concise, too:

roster_van_all_columns_paired <- 
    roster_van %>% 
    merge(roster_van_copy) %>% 
    filter(player_name != player_name_paired)

I imagine this will leave you with more columns than necessary, but they are very easy to remove with a select(-c(<col_x:col_y)) after all.

roster_van_all_columns_paired
> roster_van_all_columns_paired
  team team_num  player_name player_position team_of_interest team_paired team_num_paired player_name_paired
1  VAN        8  CHRIS.TANEV               D       BOS BRUINS         VAN               5         LUCA.SBISA
2  VAN       14 ALEX.BURROWS               L       CGY FLAMES         VAN               5         LUCA.SBISA
3  VAN        5   LUCA.SBISA               D         SL BLUES         VAN               8        CHRIS.TANEV
4  VAN       14 ALEX.BURROWS               L       CGY FLAMES         VAN               8        CHRIS.TANEV
5  VAN        5   LUCA.SBISA               D         SL BLUES         VAN              14       ALEX.BURROWS
6  VAN        8  CHRIS.TANEV               D       BOS BRUINS         VAN              14       ALEX.BURROWS
  player_position_paired team_of_interest_paired
1                      D                SL BLUES
2                      D                SL BLUES
3                      D              BOS BRUINS
4                      D              BOS BRUINS
5                      L              CGY FLAMES
6                      L              CGY FLAMES

Base R approach could look like this:

roster.van.all.copy.baseR <- merge(roster_van, roster_van_copy)
roster.van.all.baseR <- roster.van.all.copy.baseR[ which(roster.van.all.copy.baseR$player_name != roster.van.all.copy.baseR$player_name_paired), ]

Upvotes: 1

Related Questions