[R] If loops

Liaw, Andy andy_liaw at merck.com
Sun Oct 3 15:46:45 CEST 2004


> From: Laura Collins
> 
> Hi,
> 
> I'm a complete beginner to all this so I was hoping someone could help
> me!
> 
> What I'm trying to do is to write a function that returns the
> coordinates where a vector x is equal to a.  So say I invent 
> a vector x:
> 
> x<-c(,5,8,9,8,3).
> 
> If a is a<-8.
> 
> I want the function to return the coordinates of x where the number 8
> appears (i.e. 2 4).
> 
> I know I need to set up an if loop but I'm really not sure how to do
> this.

You don't if you use R (or S-PLUS):

which(x %in% a)

or even something like

x.good <- x[! x %in% a]

HTH,
Andy

> Any advice or clues will be much appreciated.
> 
> Thanks,
> Laura




More information about the R-help mailing list