[R] how to do Splus compare() function in R

Bellinger Instruments P/L lab at bellinger.com.au
Fri Apr 21 05:01:35 CEST 2006

The sign() function requires one argument,
and returns the sign of the value, where I require a number
-1, 0, or 1 depending on if the value is <, ==, or > the 
first argument.

This is the Splus function when listed,
> compare
function(e1, e2)
.Internal(compare(e1, e2), "do_op", T, 18)

unfortunately I do not understand the .internal function
and cannot find any description of this type of programming
on the CRAN site

This is how I am using it in the source file. 

zz <- readline()                # keyboard input eg: f15

if (charmatch(substring(zz,1,1),"f",  
        nomatch=-1) >0){
        ii <- as.numeric(substring(zz,2,99))
        iii <- compare(ii*1e9,f) # f is a vector of length(146) frequencies 
                                 # compare() searches thru f and
		       	         # creates vector iii with 
			               # -1 if numeric is < f[i],  
			               #  0 if numeric is == f[i], and
				         #  1 if numeric is > f[i]

	# the end result is a vector(iii) length(146) 
      # with -1,0,1 if the numeric is in f

       i <- match(-1,c(iii,-1))-1 # if vector(iii) contains 0 assign the

        # error traps for if 0 is not in vector(iii)

        if (i<1){cat("Min n = 1\n")
	          i <- 1}
        if (i>length(f)){cat("Max n =",length(f),"\n")
		  i <- length(f)} 

-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] 
Sent: Thursday, 20 April 2006 4:02 PM
Subject: Re: [R] how to do Splus compare() function in R

You would need to tell us what it does (and what the inputs are).

I think it is likely that compare(x, y) in S-PLUS is the same as sign(x-y)
in R, at least with numeric vector inputs.

Bob Kelly			lab at bellinger.com.au
Metrologist & Microwave Eng.	http://www.bellinger.com.au
Bellinger Instruments Pty Ltd 	Tel: 612 9684 1442
4 Muriel Ave			Fax: 612 9638 4435
Rydalmere NSW
Australia  2116

More information about the R-help mailing list