Goal
count the number of plus signs in the following model (character string) using base R
y ~ z + x1 + x2 + x3 + x4 + x5 + x6 + x8 + x9 + x10 + x11
Source code
model <- as.formula("y~z+x1+x2+x3+x4+x5+x6+x8+x9+x10+x11")
model
me <- gsub("y ~ ", "", model)
me
me2 <- length(unlist(gregexpr('+', me)))
me2
Output
y ~ z + x1 + x2 + x3 + x4 + x5 + x6 + x8 + x9 + x10 + x11
[1] "~"
[2] "y"
[3] "z + x1 + x2 + x3 + x4 + x5 + x6 + x8 + x9 + x10 + x11"
[1] 55 why does this not equal 11?