How to get coefficients of polynomial expression
问题 I used rSymPy and obtained following expression: "1 - 0.7*B - 0.3*B**2" Now I want to extract the coefficients of B and Coefficients of B^2, and stored in a matrix. I tried gsub function in R, any suggestions? 回答1: Do you mean like: > x <- "1 - 0.7*B - 0.3*B**2" > m <- gregexpr("[0-9]+\\.[0-9]+",x) > out <- unlist(regmatches(x,m) ) > out [1] "0.7" "0.3" A more complex example: > x <- c("1 - 0.7*B - 0.3*B**2", "1 - 0.3*B - 0.7*B**2","1 - 1.3*B - 0.6*B**2") > m <- gregexpr("[0-9]+\\.[0-9]+",x)