concatenating text within a function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

concatenating text within a function

L... L...
Dear all, I have the following variables:
fc <- quote(sqrt(2) * pi ^ (-0.1e1 / 0.2e1) * (x / theta) ^ alpha * alpha / x * exp(-(x / theta) ^ (2 * alpha) / 2))
and
d2 <- D(D(fc, "alpha"), "alpha")
I would like to create a function formed by the product of fc and d2. I tried:
fcd2 <- function(x) {}
body(fcd2) <- c(d2, "*", fc)
but I did not succeed. Any suggestion is welcome.
Best regards
ML    
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: concatenating text within a function

David Winsemius

> On Oct 1, 2016, at 7:45 AM, L... L... <[hidden email]> wrote:
>
> Dear all, I have the following variables:
> fc <- quote(sqrt(2) * pi ^ (-0.1e1 / 0.2e1) * (x / theta) ^ alpha * alpha / x * exp(-(x / theta) ^ (2 * alpha) / 2))
> and
> d2 <- D(D(fc, "alpha"), "alpha")
> I would like to create a function formed by the product of fc and d2. I tried:
> fcd2 <- function(x) {}
> body(fcd2) <- c(d2, "*", fc)

Try instead using bquote to get an expression object:

> bquote( .(fc) * .(d2) )
sqrt(2) * pi^(-1/2) * (x/theta)^alpha * alpha/x * exp(-(x/theta)^(2 *
    alpha)/2) * ((sqrt(2) * pi^(-1/2) * ((x/theta)^alpha * log((x/theta)) *
    log((x/theta))) * alpha + sqrt(2) * pi^(-1/2) * ((x/theta)^alpha *
    log((x/theta))) + sqrt(2) * pi^(-1/2) * ((x/theta)^alpha *
    log((x/theta))))/x * exp(-(x/theta)^(2 * alpha)/2) - (sqrt(2) *
    pi^(-1/2) * ((x/theta)^alpha * log((x/theta))) * alpha +
    sqrt(2) * pi^(-1/2) * (x/theta)^alpha)/x * (exp(-(x/theta)^(2 *
    alpha)/2) * ((x/theta)^(2 * alpha) * (log((x/theta)) * 2)/2)) -
    ((sqrt(2) * pi^(-1/2) * ((x/theta)^alpha * log((x/theta))) *
        alpha + sqrt(2) * pi^(-1/2) * (x/theta)^alpha)/x * (exp(-(x/theta)^(2 *
        alpha)/2) * ((x/theta)^(2 * alpha) * (log((x/theta)) *
        2)/2)) + sqrt(2) * pi^(-1/2) * (x/theta)^alpha * alpha/x *
        (exp(-(x/theta)^(2 * alpha)/2) * ((x/theta)^(2 * alpha) *
            (log((x/theta)) * 2) * (log((x/theta)) * 2)/2) -
            exp(-(x/theta)^(2 * alpha)/2) * ((x/theta)^(2 * alpha) *
                (log((x/theta)) * 2)/2) * ((x/theta)^(2 * alpha) *
                (log((x/theta)) * 2)/2))))

> fcd2 <- function(x) {}
> body(fcd2) <- bquote( .(fc) * .(d2) )
> fcd2
function (x)
sqrt(2) * pi^(-1/2) * (x/theta)^alpha * alpha/x * exp(-(x/theta)^(2 *
    alpha)/2) * ((sqrt(2) * pi^(-1/2) * ((x/theta)^alpha * log((x/theta)) *
    log((x/theta))) * alpha + sqrt(2) * pi^(-1/2) * ((x/theta)^alpha *
    log((x/theta))) + sqrt(2) * pi^(-1/2) * ((x/theta)^alpha *
    log((x/theta))))/x * exp(-(x/theta)^(2 * alpha)/2) - (sqrt(2) *
    pi^(-1/2) * ((x/theta)^alpha * log((x/theta))) * alpha +
    sqrt(2) * pi^(-1/2) * (x/theta)^alpha)/x * (exp(-(x/theta)^(2 *
    alpha)/2) * ((x/theta)^(2 * alpha) * (log((x/theta)) * 2)/2)) -
    ((sqrt(2) * pi^(-1/2) * ((x/theta)^alpha * log((x/theta))) *
        alpha + sqrt(2) * pi^(-1/2) * (x/theta)^alpha)/x * (exp(-(x/theta)^(2 *
        alpha)/2) * ((x/theta)^(2 * alpha) * (log((x/theta)) *
        2)/2)) + sqrt(2) * pi^(-1/2) * (x/theta)^alpha * alpha/x *
        (exp(-(x/theta)^(2 * alpha)/2) * ((x/theta)^(2 * alpha) *
            (log((x/theta)) * 2) * (log((x/theta)) * 2)/2) -
            exp(-(x/theta)^(2 * alpha)/2) * ((x/theta)^(2 * alpha) *
                (log((x/theta)) * 2)/2) * ((x/theta)^(2 * alpha) *
                (log((x/theta)) * 2)/2))))



> but I did not succeed. Any suggestion is welcome.
> Best regards
> ML    
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.