Two answers for you
a <- data.frame(direction = c("?????+-+-", "?+-+-????","?-+-+??-+"),
stringAsFactors = F)
a$return <- lengths(regmatches(a$direction, gregexpr("\\?", a$direction)))
or as per comments
a$return <- nchar(gsub("[^?]", "", a$direction))
Both return
'data.frame': 3 obs. of 2 variables:
$ direction: chr "?????+-+-" "?+-+-????" "?-+-+??-+"
$ return : int 5 5 3
There are tons of ways to do this depends on what you're looking for.
Update
While it may not be base R, the packages in the tidyverse are useful for data wrangling and can be used to string together a few calls easily.
install.packages("dplyr")
library(dplyr)
df <- data.frame(Direction = c("???????????-?", "???????????+?", "???????????+?", "???????????-?"), stringsAsFactors = F)
df %>%
mutate(qmark = nchar(gsub("[^?]", "", Direction)),
pos = nchar(gsub("[^+]", "", Direction)),
neg = nchar(gsub("[^-]", "", Direction)),
qminus = qmark-(pos+neg),
total = nchar(Direction))
Direction qmark pos neg qminus total
1 ???????????-? 12 0 1 11 13
2 ???????????+? 12 1 0 11 13
3 ???????????+? 12 1 0 11 13
4 ???????????-? 12 0 1 11 13
If your dataset is 10 million lines long however, you might want to use stringi
based on some benchmark testing.
install.packages("stringi")
library(stringi)
df %>%
mutate(qmark = stri_count(Direction, fixed = "?"),
pos = stri_count(Direction, fixed = "+"),
neg = stri_count(Direction, fixed = "-"),
qminus = qmark-(pos+neg))