Sum list of characters using R

r, regex, string

Solution

How about this

runcount<-function(x) {
    # find double digits
    m <- gregexpr("\\(\\d+\\)",x)
    dd <- regmatches(x,m)
    # remove double digits
    regmatches(x,m)<-""
    # remove x's
    x <- gsub("x","",x)
    # sum numbers
    # add back in double digit values (remove parens)
    sapply(strsplit(x,""), function(x) sum(as.numeric(x))) + 
      sapply(dd, function(x) sum(as.numeric(substr(x,2,nchar(x)-1))))
}

runcount("10030(11)02x")
# [1] 17

runcount("10030(11)(12)2x")
# [1] 29

runcount("100301020")
# [1] 7

runcount(c("10030(11)02x","10030(11)(12)2x","100301020"))
# [1] 17 29  7

Problem

I'm playing around with some box score data that I downloaded from retrosheet.org. Instead of providing a run total for the home and away team, the data provides a line score in the following format: `"10030(11)02x"` where each digit represents an inning. A number in `()` indicates more than 9 runs scored in an inning and `x` represents a half inning in which the team did not bat (the home team was ahead at the bottom of the 9th inning). I'm trying to figure out a way to systematically sum up the total runs using a function. Ideally I could run something like this: ``` f("10030(11)02x") = 17 ``` I'm using `sum(sapply(strsplit("10001000x", ""), as.numeric), na.rm=T)` to compute a sum for all observations that don't contain an inning with double digits, but I'm struggling figuring out how to deal with the double digit innings and parenthesis.

Original source