Re: [R] writing a simple function

From: <Bill.Venables_at_csiro.au>
Date: Thu, 14 Feb 2008 00:53:56 +1000

Your code looks like matlab, but I don't see how it would work even there.

The specific problem is the to refer to an array, you do so with square brackets [..] and not round ones, (though your v is not an array, anyway, so your troubles are just starting).

Let's tackle what I think may be your problem directly. You say you want to know if the interval (a,b) overlaps (c,d) or not.

OK, suppose a, b, c and d are four vectors defining intervals (a[i],b[i]) and (c[i],d[i]), and you want a vector of logicals as the result. Here is a suggestion.

overlap <- function(a, b, c, d)

        !((pmax(a, b) < pmin(c, d)) | (pmax(c, d) < pmin(a, b)))

Here is a little demo

> set.seed(2008)
> w <- runif(10)
> x <- runif(10)
> y <- runif(10)
> z <- runif(10)
> o <- overlap(w, x, y, z)
> data.frame(a=w, b=x, c=y, d=z, o)

           a b c d o

1  0.5711326 0.32063318 0.02002140 0.3581223  TRUE
2  0.6422710 0.09871573 0.02017792 0.6659485  TRUE
3  0.4645480 0.38348609 0.57137732 0.5682764 FALSE
4  0.8695450 0.99505570 0.78598674 0.8700771  TRUE
5  0.6338629 0.62817830 0.03717119 0.6008403 FALSE
6  0.3897689 0.34575269 0.70860446 0.7215733 FALSE
7  0.5236651 0.91862887 0.27005734 0.6000776  TRUE
8  0.7730075 0.02215747 0.76042526 0.9626952  TRUE
9  0.6267855 0.64686826 0.81222122 0.6340692  TRUE
10 0.1752022 0.66873049 0.58638984 0.1629897  TRUE

>

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA

Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile:                         +61 4 8819 4402
Home Phone:                     +61 7 3286 7700
mailto:Bill.Venables_at_csiro.au
http://www.cmis.csiro.au/bill.venables/

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of mohamed nur anisah
Sent: Thursday, 14 February 2008 12:22 AM To: r-help_at_r-project.org
Subject: [R] writing a simple function

Dear lists,    

  any suggestion on how to write a function to return a TRUE if interval [a,b] overlaps the interval [c,d]. I've tried it but an error occur saying that 'could not find function v ; in addition warning message occur'. Below is my codes.Please help me sort this problem as i'm in process of learning of writing a function. Many thanks    

  overlap<-function(m,n){
t=length(m)
v=length(n)
tt=logical(t)
tv=logical(v)
 for(i in 1:m){
  for(i in 1:n){
  if(v(i,j)<=t(i,j)){

     tt=T
     tv=T

   }
  }
 }
k=c(tt,tv)
return(k)
}    

  Regards,
  Anisah        


        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
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.

R-help_at_r-project.org mailing list
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. Received on Wed 13 Feb 2008 - 15:27:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 13 Feb 2008 - 15:30:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive