I wrote a function as:
gener1 <- function(du){
nth <- paste0(paste0("dum", 1:du, " * ", "X", 1:du), collapse = " + ")
return(nth)
}
it returns a sequence as:
"dum1 * X1 + dum2 * X2 + dum3 * X3"
Now I want to use this sequence in a next function. Simply I can just copy that and paste:
S = quote(dum1 * X1 + dum2 * X2 + dum3 * X3)
Result:
dum1 * X1 + dum2 * X2 + dum3 * X3
It works. I wonder if it is possible to automatize that I do not need to use a "copy paste approach".
The final product I want to achieve is to use S in the following situation:
fo <- substitute(BH100 ~ S * ((1 - exp(-b2*TIME))/(1-exp(-b2*100)))^b3, list(S = S))
nls(fo, data = dane, start = list(b2 = 0.01, b3 = 1.1, X1 = 20, X2 = 20, X3 = 20))
Here is an example of the data structure I have
year T BH100 TIME dum1 dum2 dum3 dum4
1987 25 12.6 25 1 0 0 0
1990 28 14.9 28 1 0 0 0
1994 32 18.8 32 1 0 0 0
1983 21 13.4 21 0 1 0 0
1986 24 16.1 24 0 1 0 0
1990 28 19.6 28 0 1 0 0
1998 36 26.7 36 0 1 0 0
2002 40 27.8 40 0 1 0 0
1994 32 17.2 32 0 0 1 0
1998 36 19.4 36 0 0 1 0
2002 40 23.5 40 0 0 1 0
2008 46 26.3 46 0 0 1 0
2013 51 28.7 51 0 0 1 0
1985 23 14.6 23 0 0 0 1
1989 27 18.5 27 0 0 0 1
1990 28 19.2 28 0 0 0 1
I'm not sure I understand your question fully, but I think you're looking for the
eval
function. Yourgener1
function right now is generating a string:"dum1 * X1 + dum2 * X2 + dum3 * X3"
. You can evaluate this string usingeval
withparse
:So, for your example, you can generate an expression string using your
gener1
function, and then use the evaluated output in yourfo
variable: