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.
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