[R] And Statement for two if functions

R. Michael Weylandt michael.weylandt at gmail.com
Mon Jan 30 21:21:16 CET 2012


Nested if's are fine in R, but as David said you probably want
ifelse(). This sounds sufficiently homework-y that I'm hesitant to
give example code but it's all over the archives.

Just to head off a problem I see in your pesudo-code; you're going to
want to use ifelse() to construct the points vector and then assign
it: it's terribly dangerous to do assignment within ifelse() as if it
were a simple if().

Michael


On Mon, Jan 30, 2012 at 1:55 PM, kerry1912 <kerry1912 at hotmail.com> wrote:
> Sorry that post was written in a bit if a rush.
>
> I am writing a function in which I am trying to create a league table from a
> data frame of rugby matches with the columns as follows: home team, away
> team, home score and away score.
>
> In rugby you can get an extra bonus point if you are the losing team and
> lose by less than 7 points. So therefore in my function I am writing if the
> away team loses AND loses by less than or equal to 7 points then the away
> team will get an extra point,
>
> So ideally want to write:
>
> if(games[i,3] > games[i,4]  AND games[i,3] <= games[i,4] + 7) {
>                                T[which(teams == games[i,2]),"Points"] <-
>                                                        T[which(teams == games[i,2]),"Points"] + 1}
>
> Which is inset into a function in R where the input of the function is
> 'games' which will be the list of the 132 matches of rugby being analysed
> and where teams is the list of 12 teams in the league.
>
> I wasn't sure if it was possible to write an 'if' function embedded in
> another 'if' function or which method would be best to achieve this.
>
> Thank you.
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/And-Statement-for-two-if-functions-tp4341179p4342098.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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list