# [R] deriv; loop

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Jul 31 12:13:45 CEST 2007

```francogrex wrote:
> Hi, 2 questions:
>
> Question 1: example of what I currently do:
>
> for(i in 1:6){sink("temp.txt",append=TRUE)
> dput(i+0)
> sink()}
> x=scan(file="temp.txt")
> print(prod(x))
> file.remove("C:/R-2.5.0/temp.txt")
>
> But how to convert the output of the loop to a vector that I can manipulate
> (by prod or sum etc), without having to write and append to a file?

So, do you want the file at the end or not? If not:

x <- 1:6
prod(x)

and if this is really the solution you want, the please read the posting
guide and the manuals before posting again.

> Question 2:
>
>> deriv(~gamma(x),"x")
>
> expression({
>     .expr1 <- gamma(x)
>     .value <- .expr1
>     .grad <- array(0, c(length(.value), 1), list(NULL, c("x")))
>     .grad[, "x"] <- .expr1 * psigamma(x)
>     .value
> })
>
> BUT
>
>> deriv3(~gamma(x),"x")
> Error in deriv3.formula(~gamma(x), "x") : Function 'psigamma' is not in the
> derivatives table
>
> What I want is the expression for the second derivative (which I believe is
> trigamma(x), or psigamma(x,1)), how can I obtain that?

By using some algebraic software (rather than a numeric one) or
contributing complete derivatives tables for the next R release.

Uwe Ligges

```