How to make function which can read an argument without any gap or comma?

Please be clear about what you a looking for when you ask a question here, otherwise it is too hard for us to figure out what you are looking to accomplish. For example you should present an example as something like:

weights <- c(A = 2, B= 1, C = 9, H = 5)
mw(ACHHB)
>22

That is what your input data is, what the function you need looks like, and what it produces. If you have a non-working function show that too.

You should also look at how to make reprex's as that will allow us to try your code example in the same context as you and see the errors it produces.

In any case to do that you will have to use the tidyverse and quosures.

suppressPackageStartupMessages(library(tidyverse))
weights <- c(A = 2, B= 1, C = 9, H = 5)

mw3 <- function(S) {
    qs <- rlang::enquo(S)
    s <- rlang::expr_text(qs[[2]])
    sum(1:str_length(s) %>% map_dbl(~ weights[[str_sub(s, ., .)]]))
}

mw3(ACHHB)

#> [1] 22

Created on 2018-02-27 by the reprex package (v0.2.0).

1 Like