# [R] Converting a function from Splus to R

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Feb 29 14:40:57 CET 2012

On 29/02/2012 13:24, R. Michael Weylandt wrote:
> Change the name to something syntactically valid? The problem is that
> you can't (well, you can, but it's ill advised) have variable names
> beginning with numbers.  They don't seem to be used much so there
> won't be much trouble in that.

I think that 2.0d0 is someone translating C into R. Just use 2 .  In R,
2 is a double precision floating-point constant: in C and (recent
S-PLUS) it is integer and you need to use 2.0 .

> Michael
>
> 2012/2/29 Freddy Hernández<fhernanb at gmail.com>:
>> I have a function written for Splus, when I run it in R I obtain get an error
>> because the function has the elements "0.d0" and "2.d0". How can I change it
>> to run in R?
>>
>> The function can be found in page 230 from
>>   http://www.stat.wisc.edu/~mchung/teaching/stat471/stat_computing.pdf
>>
>> Function is as follows:
>>
>> gauher<- function(n) {# Gauss-Hermite: returns x,w so that
>> #\int_-\infty^\infty exp(-x^2) f(x) dx \doteq \sum w_i f(x_i)
>> EPS<- 3.e-14
>> PIM4<- .7511255444649425D0
>> MAXIT<- 10
>> m<- trunc((n+1)/2)
>> x<- w<- rep(-1,n)
>> for (i in 1:m) {
>> if (i==1) {
>> z<- sqrt(2*n+1)-1.85575*(2*n+1)^(-.16667)
>> } else if(i==2) {
>> z<- z-1.14*n^.426/z
>> } else if (i==3) {
>> z<- 1.86*z-.86*x[1]
>> } else if (i==4) {
>> z<- 1.91*z-.91*x[2]
>> } else {
>> z<- 2.*z-x[i-2]
>> }
>> for (its in 1:MAXIT) {
>> p1<- PIM4
>> p2<- 0.d0
>> for (j in 1:n) {
>> p3<- p2
>> p2<- p1
>> p1<- z*sqrt(2.d0/j)*p2-sqrt((j-1)/j)*p3
>> }
>> pp<- sqrt(2.d0*n)*p2
>> z1<- z
>> z<- z1-p1/pp
>> if(abs(z-z1)<= EPS) break
>> }
>> x[i]<- z
>> x[n+1-i]<- -z
>> w[i]<- 2/(pp*pp)
>> w[n+1-i]<- w[i]
>> }
>> list(x=x,w=w)
>> }
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Converting-a-function-from-Splus-to-R-tp4431416p4431416.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595