I have a data frame with a string variable that contains two sets of numbers. I need to multiply one number by the next number and assign the result to another field. This would be straightforward to do with regex, but the problem is that some of the observations contain multiple inputs that will need to be calculated, and I am struggling to conceptualize how to iterate over these strings until there are no additional pairs to multiply. And then once all pairs have been multiplied, they must be added and assigned to the new variable.
Here is my raw data
V1 <- c("ABC01-3XYZ=2, ABC04-5XYZ=3, ABC06-7XYZ=1",
"ABC04-5XYZ=2", "ABC01-3XYZ=1, ABC04-5XYZ=1")
df <- data.frame(V1)
V1
1 ABC01-3XYZ=2, ABC04-5XYZ=3, ABC06-7XYZ=1
2 ABC04-5XYZ=2
3 ABC01-3XYZ=1, ABC04-5XYZ=1
I would like to multiple the integer immediately following the "-" by the integer immediately following the "=", and then sum them, so that the final result looks like this:
V1 V2
1 ABC01-3XYZ=2, ABC04-5XYZ=3, ABC06-7XYZ=1 28
2 ABC04-5XYZ=2 10
3 ABC01-3XYZ=1, ABC04-5XYZ=1 8
Any suggestions about how to iterate past each comma would be greatly appreciated. Thanks!
,