How can I make sure that the last entry of the vector is non-zero?
# Current output
polycoefs("-x^{3} 3x^5-x-3x^3- 3x^5 - 1")
x^0 x^1 x^2 x^3 x^4 x^5
-1 -1 0 -4 0 0
# Intended output
polycoefs("-x^{3} 3x^5-x-3x^3- 3x^5 - 1")
x^0 x^1 x^2 x^3
-1 -1 0 -4
CodePudding user response:
If:
result = polycoefs("-x^{3} 3x^5-x-3x^3- 3x^5 - 1")
x^0 x^1 x^2 x^3 x^4 x^5
-1 -1 0 -4 0 0
If you want to make the last entry non-zero:
while(tail(result,1) == 0){
result = result[-length(result)]
}
That:
> result
x^0 x^1 x^2 x^3
-1 -1 0 -4
You can add it to your function just before returning result.
If you want to remove all 0s you can do (Idk why keeping x2):
> result[-which(result == 0, arr.ind=TRUE)]
x^0 x^1 x^3
-1 -1 -4