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. Any advice or clues will be much appreciated. Thanks, Laura [[alternative HTML version deleted]]
Laura Collins wrote:> 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. > > > > Any advice or clues will be much appreciated.which(a==x) Please read "An Introduction to R", the R FAQ and learn to use the help system as well as the mailing list archives (as described in the Posting Guide cited below at the end of your). Uwe Ligges> > > Thanks, > > > > Laura > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 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
Dear Laura,> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Laura Collins > Sent: Sunday, October 03, 2004 8:32 AM > To: R-help at stat.math.ethz.ch > Subject: [R] If loops > > 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. > > > > Any advice or clues will be much appreciated. >> which(x == 8)[1] 2 4 See ?which. Note that your definition of x is in error (try it) and that <- in R means assignment, not equality. John