How do I count trailing zeros in a vector of string. For example, if my vector of string is:
x = c('0000','1200','1301','X230','9900')
The answer should be
> numZeros
[1] 4 2 0 1 2
I do not want to use multiple ifelse
as I think a more elegant and faster solution should be present. I tried using modulus, like this
y = as.integer(x)
numZeros = (!(y%%10000))+(!(y%%1000))+(!(y%%100))+(!(y%%10))
but that would require two conditions to be true.
- Maximum length of the string is fixed (which is true in my case) and
- All the strings in the vector are convertible to integers, which is not true in my case.
Then used stringr
package and created a solution but it is very lengthy.
library(stringr)
numZeros =
4*str_detect(x,"0000") +
3*str_detect(x,"[1-9 A-Z]000") +
2*str_detect(x,"[1-9 A-Z]{2}00") +
str_detect(x,"[1-9 A-Z]{3}0")
Also, I can't figure out whether str_detect
uses ifelse
by looking at definition of str_detect
.
I found same question here but for python. If this has been answered for R, please provide the link.