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

#1

I am new to R and i want to make a function where i want my function to read my input as ABCDEF.... Please tell me how can i make this function where my argument is like this. I am able to make a function with input like this A,B,C,D,E,F...

How to make a function which has continuous argument (without any gap or commas)
How to make a function which has continuous argument (without any gap or commas)
#2

Can you show what you already have? I'm not sure I understand what the exact issue here.

#4

It is generally frowned upon to post a picture of your code. Please create a `reprex` so that the community is better able to help you:

#5

function1=function(char1, char2...){
result=sum(char1, char2...)
return(result)
}

This is what i have made

#6

I want my function to read the argument and sum up the values which I've already saved in that

#7

I give your function 2 values: `A` and `B`. What should be the result?

#8

Suppose i have stores A=1 B=2 then by using this function our output would be 3

#9

What's wrong with `sum()`?

#10

I am making a function in which it will count the molecular weight of a protein, for which i have already stored the individual molecular weight of every amino acid.
I want my function to read and show me the aggregation of the input (all the amino acid which i have entered as an input).
I have use this "Sum" to sumup the total molecular weight of amino acids

#11

Well, the function `sum()` calculates the total of its inputs.

If that's not what you are after, then you'll have to post a reprex.

#12

Yes i am using sum for this purpose only, i just want your help to tell me how can i make a program for ABCDEF type of input or argument without any gap or commas. I want to make an argument in string not in character, so please guid me in this. Thankyou

Calculate the sum of all the characters
#13

There are many ways to do this. Below is one way that uses the tidyverse and another that doesn't.

The `%>%` function is for piping a value from one segment of a pipe to another.

For future reference you should also include an example that shows the input you have and the specific output you expect for that input. It would make it easier for use to understand what you are trying to accomplish if you did that.

``````suppressPackageStartupMessages(library(tidyverse))

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

s <- "ACHHB"

# using the tidyverse
sum(1:str_length(s) %>% map_dbl(~ weights[[str_sub(s, ., .)]]))
#> [1] 22

# without the tidyverse, i.e. base R
sum(sapply(1:nchar(s), function(n) {weights[[substr(s, n, n)]]}))
#> [1] 22
``````

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

#14

Thank you so much but just tell me how can i use string as an argument where the input is of multiple without any gap or commas or anything

#15

(library(tidyverse))

mw<- function(char1, char2,...){
result<-sum(char1, char2,...)
return(result)
}

This ia what i have made but i want my argument without any commas which i can not made plhelp me with this

#16

It's important to show what you are trying to do. Giving the function you are looking for is a good start, even if that function doesn't work. But you also need to show using that function and what you expect the result to be.

I think this is what you might be looking for.

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

mw <- function(...){
sum(sapply(c(...), function(c) {weights[[c]]}))
}

mw("A", "C", "H", "H", "B")
#> [1] 22
``````

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

#17

Thankyou for this help but i am looking for -

mw(ABCD) this type of input for my function

#18

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).

How to add formula in a function?
#19

I am sorry for the trouble you have faced because of me, i am new here that's why! And thankyou for this but in my R it is showing me that they cant find %>% this function how to solve this

#20

Are you doing a`library(tidyverse)` at the beginning of your code? If you are using R Studio you can open the packages tab and check off tidyverse instead.

#21

Thank you so much for your help it means a lot!