0
a="1-B*(0.5+c1)+B**2*(-c2+0.5*c1)+0.5*c3*B**3"

I want to breakdown the above character string as

B, -(0.5+c1), B**2, (-c2+0.5c1), B**3, 0.5*c3

using R. Any suggestions?

wxyz
  • 35
  • 9
  • 1
    Your output seems somewhat arbitrary. Perhaps you should state your rules. Why does the `1` in `1-B` disappear? Why do the negative sign for `-B` go to the bracket to become `-(0.5+c1)`? Why do some of the "factors" change order? – Anders Ellern Bilgrau Jul 12 '16 at 08:29
  • Expression is a polynomial of B, I want to extract orders of B separately and the corresponding coefficients separately – wxyz Jul 12 '16 at 08:32
  • 1
    I suggest you edit the title to get rid of regular expressions. Regex is one way of solving the problem (with some potential pitfalls) but might not be the only way. The title should say something like "extract coefficients of polynomial expression" because that is what you are trying to do – C8H10N4O2 Jul 12 '16 at 12:47

1 Answers1

1

While not exactly what you want as output, the following is sufficiently close and might help you solve the problem:

a <- "1-B*(0.5+c1)+B**2*(-c2+0.5*c1)+0.5*c3*B**3"
b <- gsub("**", "^", a, fixed = TRUE)  # Replace "**" with "^" to simply the regex

ans <- unlist(strsplit(b, '\\([^)]*\\)(*SKIP)(*F)|\\*|\\+', perl = TRUE))
print(ans)
#[1] "1-B"       "(0.5+c1)"     "B^2"     "(-c2+0.5*c1)" "0.5"          "c3"          
#[7] "B^3"

More on the regular expression used can be found here. You can easily change the "^" back to "**" using gsub.

I see no easy way to attach the negative sign of B to the coefficient.

Community
  • 1
  • 1
Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37