I have a dataframe from a chess game that contains the following data:
>View(game_summary_df)
white_player black_player result white_elo black_elo
1 [White Gyc12] [Black SuperCarp] [Result 1/2-1/2] [WhiteElo 1355] [BlackElo 1243]
I would like to clean up the data by removing, for instance, "[White " and "]" from the white_player column, leaving only "Gyc12".
I am able to accomplish this using two separate str_replace commands:
> str_replace(game_summary_df[1,1], "\\[White ", "")
> str_replace(game_summary_df[1,1], "\\]", "")
Is there a way to combine this into one str_replace command? Something like the opposite of . ? to place in the middle is my thought.
CodePudding user response:
To do this cleanup in a single function call, you may use gsub()
with an alternation. For example:
white_player <- "[White Gyc12]"
output <- gsub("^\\[\\w \\s |\\]$", "", white_player)
output
[1] "Gyc12"
CodePudding user response:
game_summary_df[1,1] %>% str_extract((?<=White ).*(?=\\]))