Re: R-help Digest, Vol 203, Issue 8

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

Re: R-help Digest, Vol 203, Issue 8

Helmut Schütz
Dear Hans,

[hidden email] wrote on 2020-01-09 12:00:
> Date: Wed, 8 Jan 2020 12:09:55 +0100
> From: Hans W Borchers <[hidden email]>
> To: R help project <[hidden email]>
> Subject: [R] Which external functions are called in a package?
> [Solved]
>
> NB: `trapz`, ie.
> the trapezoidal integration formula, seems to be the numerical
> function to be missed the most in R base.

In R base indeed. However available in Frank Harrels Hmisc as the
function trap.rule(x, y) for sorted values.
In plain R: function(x, y) sum(diff(x) * (y[-1] + y[-length(y)]))/2

Helmut

--
Ing. Helmut Schütz
BEBAC – Consultancy Services for
Bioequivalence and Bioavailability Studies
Neubaugasse 36/11
1070 Vienna, Austria
W https://bebac.at/
F https://forum.bebac.at/

______________________________________________
[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: R-help Digest, Vol 203, Issue 8

Rui Barradas
Hello,

And there's also


#
# library(caTools)
# Author(s)
# Jarek Tuszynski <[hidden email]>
#
# Original
trapz <- function(x, y){
     idx = 2:length(x)
     return(as.double( (x[idx] - x[idx-1]) %*% (y[idx] + y[idx-1]) ) / 2)
}

# Modified by me, input is x, f(x)
trapzf <- function(x, FUN) trapz(x, FUN(x))
# Call like 'integrate'
trapzf2 <- function(f, lower, upper, subdivisions = 100){
     trapzf(seq(lower, upper, length.out = subdivisions), match.fun(f))
}



So I guess it's not missing, just missing in base R, like the OP said.

Hope this helps,

Rui Barradas

Às 11:20 de 09/01/20, Helmut Schütz escreveu:

> Dear Hans,
>
> [hidden email] wrote on 2020-01-09 12:00:
>> Date: Wed, 8 Jan 2020 12:09:55 +0100
>> From: Hans W Borchers <[hidden email]>
>> To: R help project <[hidden email]>
>> Subject: [R] Which external functions are called in a package?
>>     [Solved]
>>
>> NB: `trapz`, ie.
>> the trapezoidal integration formula, seems to be the numerical
>> function to be missed the most in R base.
>
> In R base indeed. However available in Frank Harrels Hmisc as the
> function trap.rule(x, y) for sorted values.
> In plain R: function(x, y) sum(diff(x) * (y[-1] + y[-length(y)]))/2
>
> Helmut
>

______________________________________________
[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.