How to add formula in a function?


#1

I would like to make a function without installing any external package. In this function i would like to calculate a pI value of amino acids. Formula for calculating a pI value is
pI= 1/2(alpha carboxy+alpha amino)

regarding this, i have following data.

alpha_carboxy=c(I=2.36, L=2.36, K=2.18, M=2.28, F=1.83, T=2.63, W=2.38, V=2.32, R=2.17, H=1.82, A=2.34, N=2.02, D=2.09, C=1.71, E=2.19, Q=2.17, G=2.34, P=1.99, S=2.21, Y=2.2)
alpha_amino=c(I=9.68, L=9.60, K=8.95, M=9.21, F=9.13, T=9.10, W=9.39, V=9.62, R=9.04, H=9.17, A=9.69, N=8.84, D=9.82, C=10.78, E=9.67, Q=9.13, G=9.60, P=10.60, S=9.15, Y=9.11)

Input or argument should be continuous (without any gap or commas).

amino_acids =c(I, L, K, M, F, T, W, V, R, H, A, N, D, C, E, Q, G, P, S, Y)

Input should be like (ILKMFTWVRHANDCEQSY)

So, i want my function to read an argument in the format like i have just explained above and then calculate it with the help of a formula.
Thank you!


#2

This topic has been discussed here:

You should include your code i a reprex to make it easier for us to help you.

A prose description isn’t sufficient, you also need to make a simple reprex that:

  1. Builds the input data you are using.
  2. The function you are trying to write, even if it doesn’t work.
  3. Usage of the function you are trying to write, even if it doesn’t work.
  4. Builds the output data you want the function to produce.

You can learn more about reprex’s here:

Right now the is an issue with the version of reprex that is in CRAN so you should download it directly from github.

Until CRAN catches up with the latest version install reprex with

devtools::install_github(“tidyverse/reprex”)

You can’t do what you want without using a package… unless you recreate some code from the implementation of the tidyverse from scratch yourself. Some of that code is in C not R. You can see it on github in tidyverse/

This is an example that shows the basics of how to do what you want using the tidyverse/rlang.

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-03-10 by the reprex package (v0.2.0).


#3

Hello sir,
I am very grateful for your help but this topic is far different from my previous post and also i do not want to include any external package.


#4

I have not started making a function because i do not understand how to incorporate two values for a single vector.
please help me in this.


#5

If you do not want to include any external packages, then please do not post your question in #tidyverse, as it’s a bit misleading. This falls under the Keep it tidy subheader here in the guidelines:
https://community.rstudio.com/guidelines#keep-tidy