How to substract from the total number of argument?


Thanks community guys, you have helped me in my previous query.
And now, i want to learn how to substract 1(one) from the total number of the input we have entered into our function and also multiple it with some number.

For the reference, this is the function which i have got from here,

ref<- c(A=1, B=2, C=3)
carbon_nunber<-function(compound, ref){

carbon_number("AAAAA", ref=ref)

I want to learn how to substract 1(one) from the total number of input(5 in this case). So that it became 4 and then i could multiple it with some number
P.S - It should be great help if above function should not be changed due to this question


can you just use:

carbon_number("AAAAA", ref=ref) - 1?


Hello sir,
Yes i have tried this but it is not giving me the answer i want!


Are you sure the output to your function is giving you what you want?


Yes, it is reading everything correctly and giving me answer correctly, but i want to modify this only function where i could substract 1(one) from the input and then multiple it with 18. Also i wwnt the rest of the program same(it should ready and sum up the argument)


Am I correct in assuming that you mean subtract when you are saying substrate? Do you want this to happen in the function or do you want to just do it after you have the output from your function? What are you expecting to get that you are not getting when you try it?

It will be much easier to help you if you give a self-contained reprex:


Yes, you are getting me correctly, and i want that to happen in function itself. Sorry for the inconvenience.


Hi there,

Not totally clear on your expected result, but does the new function get you there?

ref <- c(A = 1, B = 2, C = 3)

carbon_number_orig <- function(compound, ref){
  compound <- unlist(strsplit(compound, ""))

carbon_number_orig("AAAAA", ref = ref)
#> [1] 5

carbon_number_new <- function(compound, ref){
  compound <- unlist(strsplit(compound, ""))
  sum(ref[head(compound, -1)])

carbon_number_new("AAAAA", ref = ref)
#> [1] 4

Created on 2018-03-01 by the reprex package (v0.2.0).

In the new function, head(compound, -1) drops the last element of the compound vector, so instead of "A", "A", "A", "A", "A", you get "A", "A", "A", "A".


Thank you for this sir, but i don’t want to use external package. Moreover its is not what i need.
Sir, i am making a function which can calculate the molecular weight of a protein for my project work.
Proteins are made up of amino acids and two amino acids are linked with peptide bonds, which in turn cause the loss of a water molecule, which i am sure you know very well than me. The function which i have mentioned earlier is doing good, but i want to add this feature too. I mean i want my function where it substract 1 from the total number of the argument or input given plus should also sum up.
For example- suppose, G stands for glysine has a molecular weight of 75, then GGGGG would be 375 but when peptide bonds plays a role here then there would be 303 (375-(5-1(18))). (18 is water) there are 5 G that’s why there are 4 peptide bonds. I hope i am able to make you understand my query.
Thank you


Is this what you are looking for?

For future reference you should make a reprex as @tbradly said before. As is you demo code has a typo in it which you would have caught when you make the reprex.

Also it is important that you are clear about what the input to your function is and what the output by example, not by description. You gave us an example input and output in your last message, you really should have done that in your first… you would have gotten an answer much sooner if you had done that and a reprex.

ref<- c(A=1, B=2, C=3)
carbon_number<-function(compound, ref){
    sum(ref[molcules]) - ((nchar(compound) - 1) * 18)

carbon_number("AAAAA", ref=ref)
#> [1] -67

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


Please excuse me for the mistakes i have been doing! I will keep that in mind for future queries. Once again, thank you so much for this help.