0
votes

I'm attempting to assign team values to each player in my data frame based on whether they're included in the "home_lineup" or "away_lineup" character strings.

I've used the str_detect function and beyond that, I haven't found alternate ways to approach the problem.

Included in this link is a snapshot of the data frame, which would have the season_data_select assignment.

library(gsheet)
url <- 'https://docs.google.com/spreadsheets/d/1Z1njK5FxCZgIUaxxoRUVaK8DFNWP4OGthT6kuFHcC7I/edit?usp=sharing'
season_data_select  <- gsheet2tbl(url)

https://docs.google.com/spreadsheets/d/1Z1njK5FxCZgIUaxxoRUVaK8DFNWP4OGthT6kuFHcC7I/edit?usp=sharing

library(dplyr)
season_data_select <- season_data_select %>% 
                      mutate(player_team = ifelse(str_detect(player, home_lineup), 
                            home, 
                            away))

Ideally, I am looking a code that will introduce a new column (player_team) that will take the value in the "player" column and check to see if it is in the "home_lineup" column. If it is, then return the value in the "home" column. If not, return the "away" column value. Alternatively, I could see it being "if in 'home_lineup', return 'home', if in 'away_lineup', return 'away'"

When used, the current code returns the value in away, which leads me to believe there is an issue when trying to reference the 'home_lineup' column with the 'player' value. All of the involved variables are in the character class.

1
Can you share the data using dput here? dput(season_data_select) to be precise. Also it would make it easier to help if you could also show the expected output for the data shared.Ronak Shah
@RonakShah Hello, I just updated the Google spreadsheet to indicate the desired outcome (highlighted in yellow). And I can't get dput to work for 3.5.3, but that spreadsheet is a sample the data frame I am using. If you can copy it into an excel file and save as a csv., it should work.Small Chimp

1 Answers

3
votes

Since str_detect is vectorised over string and pattern we can use that. Also to get new column with "home" and "away" keywords we can use the same str_detect

library(dplyr)
library(stringr)

season_data_select %>% 
     mutate(player_team = if_else(str_detect(home_lineup, player), home, away), 
            new = c("away", "home")[str_detect(home_lineup, player) + 1])