[R] problems with function-formating, please help

Duncan Murdoch murdoch at stats.uwo.ca
Sat Aug 6 12:13:38 CEST 2005


Joerg Burmester wrote:
> please help me.
> 
> since 2 days i have tried desperately to find desperately a solution for the following problem:
> 
> i have an array of 3 different vectors like:
> 
> 
>>clustertransformationstabelle[,15:17]
> 
>           x1            x2                x3
> 1  0.6270023 0.2464946 0.5074781
> 2  0.5629291 0.1783861 0.4398626
> 3  0.6773455 0.3107641 0.5637289
> 4  0.0000000 0.0000000 0.0000000
> 
> i want to calculate a corresponding result-vector with a formula resp function eg:
> 
> 
>>result <- - 1.60638946783994e-05 + 384.649339378693 * x1 - 168.920548209815 * x2 - 693.51261768584 * x3 + 479.305764753298 * x1 * x3
> 
> 
> after formating the result would look like
> 
>>dim(result) <- c(4,1)
>>result
> 
>                [,1]
>  [1,]  1.057852e-01
>  [2,]  2.854227e-02
>  [3,]  1.107150e-01
>  [4,] -1.606389e-05
> 
> fine....
> but due to mass routines, i have to create the result-function automatically, 
> unfortunately it can only be generated via a paste-command as a character-string in quotes like:
> 
> functionterm <- "- 1.60638946783994e-05 + 384.649339378693 * x1 - 168.920548209815 * x2 - 693.51261768584 * x3 + 479.305764753298 * x1 * x3"

You should almost never write text and reparse it -- it almost always 
indicates  a poor solution to whatever problem you're trying to solve.

I can't tell from your message what you want your function to do, but if 
you want a function that takes args x1, x2, x3 and returns the value 
1.60638946783994e-05 + 384.649339378693 * x1 - 168.920548209815 * x2 - 
693.51261768584 * x3 + 479.305764753298 * x1 * x3, where those 
coefficients come from some other calculation, just do it like this:

makefn <- function(coeffs) {
   function(x1, x2, x3) coeffs[1] +
                        coeffs[2]*x1 +
                        coeffs[3]*x2 +
                        coeffs[4]*x1*x3
}

Then use it like this:

f <- makefn(c(-1.606, 384.64, etc.))

and f is the function you want.

Duncan Murdoch

> 
> how on earth can i use that automatically generated functionterm (see line above), 
> unfortunately inclusive their quotes, in order to get a correct fomated function?
> 
> if i try to convert it into a function like:
> 
>>result <- as.function(alist(x1=,x2=,x3=, functionterm))
> 
> 
> and give some example input-data eg.:
> 
>>result(0.6270023, 0.2464946, 0.5074781)
> 
> 
> i always get that stupid functionterm in quotes returned
> [1] "- 1.60638946783994e-05 + 384.649339378693 * x1 - 168.920548209815 * x2 - 693.51261768584 * x3 + 479.305764753298 * x1 * x3"
> 
> instead of the correct values (like 1.057852e-01, see result-array[1,1] some lines above)
> please help me, i am blocked since 2 days already by that stupid problem
> 
> joerg burmester
> 
> Phone: +49-(0)4131-40898-5
> Mobile Phone: +49-(0)177-4812371
> Fax: +49-(0)4131-40898-7
> Email: joerg-burmester at gmx.net
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list