My question is somehow related to an already answered question Need to extract individual characters from a string column using R.
I try to solve this question with my knowledge and need to know how to remove non numeric characters in parenthesis within a string: `
This is the dataframe with column x
:
team linescore ondate x
1 NYM 010000000 2020-08-01 0, 1, 0, 0, 0, 0, 0, 0, 0
2 NYM (10)1140006x) 2020-08-02 (, 1, 0, ), 1, 1, 4, 0, 0, 0, 6, x, )
3 BOS 002200010 2020-08-13 0, 0, 2, 2, 0, 0, 0, 1, 0
4 NYM 00000(11)01x 2020-08-15 0, 0, 0, 0, 0, (, 1, 1, ), 0, 1, x
5 BOS 311200 2020-08-20 3, 1, 1, 2, 0, 0
structure(list(team = c("NYM", "NYM", "BOS", "NYM", "BOS"), linescore = c("010000000",
"(10)1140006x)", "002200010", "00000(11)01x", "311200"), ondate = structure(c(18475,
18476, 18487, 18489, 18494), class = "Date"), x = list(c("0",
"1", "0", "0", "0", "0", "0", "0", "0"), c("(", "1", "0", ")",
"1", "1", "4", "0", "0", "0", "6", "x", ")"), c("0", "0", "2",
"2", "0", "0", "0", "1", "0"), c("0", "0", "0", "0", "0", "(",
"1", "1", ")", "0", "1", "x"), c("3", "1", "1", "2", "0", "0"
))), class = "data.frame", row.names = c(NA, -5L))
Desired Output:
team linescore ondate x
1 NYM 010000000 2020-08-01 0, 1, 0, 0, 0, 0, 0, 0, 0
2 NYM (10)1140006x) 2020-08-02 10, 1, 1, 4, 0, 0, 0, 6, x, )
3 BOS 002200010 2020-08-13 0, 0, 2, 2, 0, 0, 0, 1, 0
4 NYM 00000(11)01x 2020-08-15 0, 0, 0, 0, 0, 11, 0, 1, x
5 BOS 311200 2020-08-20 3, 1, 1, 2, 0, 0
How can I change (, 1, 0, )
to 10
and (, 1, 1, )
to 11
and leave the rest as is.
Some help I already got so far:
regex for replacement of specific character outside parenthesis only thanks AnilGoyal
gsub("\\D+", "", str1)
thanks to akrungsub("[(,) ]", "", "(, 1, 0, )")
thanks to Anoushirvan
Thanks!