In the Winner column of my dataframe, I want to remove all the text starting from the left parenthesis.
Searching stackoverflow.com, I found this response, and I applied its stringr solution in my code, but it does not work. My code is not changing my input.
Input:
Year Lg Winner Team
1956 NL Don Newcombe (1 | MVP) Brooklyn (1)
1957 NL Warren Spahn (1 | HOF | ASG) Milwaukee (1)
1958 AL Bob Turley (1 | ASG) New York (1)
Here is how I want the output to look:
Year Lg Winner Team
1956 NL Don Newcombe Brooklyn (1)
1957 NL Warren Spahn Milwaukee (1)
1958 AL Bob Turley New York (1)
dput(dfx):
structure(list(Year = 1956:1958, Lg = structure(c(2L, 2L, 1L), .Label = c("AL",
"NL"), class = "factor"), Winner = structure(c(2L, 3L, 1L), .Label = c("Bob Turley (1 | ASG)",
"Don Newcombe (1 | MVP)", "Warren Spahn (1 | HOF | ASG)"
), class = "factor"), Team = structure(1:3, .Label = c("Brooklyn (1)",
"Milwaukee (1)", "New York (1)"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
Code:
library(stringr)
dfnoparens <- dfx
str_replace(dfnoparens$Winner, " \\(.*\\)", "")
head(dfnoparens)