Grokbase Groups R r-help March 2002
FAQ
Hi,

This is a simple question with if elseif....however I am having trouble
constructing the solution for some reason.

Suppose I have a data set with 3 variables, a, b and c say. Let's say c
is the sum of a and b. So:
a b c
1 2 3
2 3 5
3 4 7
. . .
. . .
. . .

Suppose that I know there have been some data entry errors and I want to
check if ALL values in c is really the sum of a and b, and if not, print
out the whole line (i.e. all values of a, b and c in that row).

Any help on how I can write this if elseif block will be apprecaited!

Ko-Kang

------------------------------------------------------------------------------
Ko-Kang Kevin Wang
Postgraduate PGDipSci Student
Department of Statistics
University of Auckland
New Zealand

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Search Discussions

  • Uwe Ligges at Mar 24, 2002 at 11:26 am

    Ko-Kang Kevin Wang wrote:
    Hi,

    This is a simple question with if elseif....however I am having trouble
    constructing the solution for some reason.

    Suppose I have a data set with 3 variables, a, b and c say. Let's say c
    is the sum of a and b. So:
    a b c
    1 2 3
    2 3 5
    3 4 7
    . . .
    . . .
    . . .

    Suppose that I know there have been some data entry errors and I want to
    check if ALL values in c is really the sum of a and b, and if not, print
    out the whole line (i.e. all values of a, b and c in that row).

    Any help on how I can write this if elseif block will be apprecaited!
    Assume your "data set" is a data.frame():

    X <- data.frame(a=1:5, b=2:6, ce=c(3,5,7,6,11))
    # Let's call it a, b, ce --- c already is a function
    # Now get the rows with errors:
    X[X$a + X$b != X$ce, ]


    So you neither need the construct
    if(condition){
    statement
    }
    else{
    statement2
    }

    nor
    elseif(condition, statement1, statement2)

    Uwe Ligges
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Ott Toomet at Mar 25, 2002 at 10:04 am

    On Sun, 24 Mar 2002, Uwe Ligges wrote:
    Ko-Kang Kevin Wang wrote:
    Hi,

    This is a simple question with if elseif....however I am having trouble
    constructing the solution for some reason.

    Suppose I have a data set with 3 variables, a, b and c say. Let's say c
    is the sum of a and b. So:
    a b c
    1 2 3
    2 3 5
    3 4 7
    . . .
    . . .
    . . .

    Suppose that I know there have been some data entry errors and I want to
    check if ALL values in c is really the sum of a and b, and if not, print
    out the whole line (i.e. all values of a, b and c in that row).

    Any help on how I can write this if elseif block will be apprecaited!
    Assume your "data set" is a data.frame():

    X <- data.frame(a=1:5, b=2:6, ce=c(3,5,7,6,11))
    # Let's call it a, b, ce --- c already is a function
    # Now get the rows with errors:
    X[X$a + X$b != X$ce, ]
    I do recommend not to use == or != to test equality. Operators == or != are usable
    == _only_ for integers. Generally, on digital computers you can never know
    when two real numbers are equal. Here is a simle example (R 1.4.0, P-II,
    linux):
    1 + 2 [1] 3
    1 + 2 == 3 [1] TRUE
    1.1 + 2.2 [1] 3.3
    1.1 + 2.2 == 3.3
    [1] FALSE

    So, using Uwe's example, you should write something like

    tolerance= .Machine$double.eps ^ 0.5
    X[abs(X$a + X$b - X$ce) > tolerance,]
    a b ce
    4 4 5 6


    Regards,

    Ott Toomet


    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Göran Broström at Mar 24, 2002 at 12:17 pm

    On Sun, 24 Mar 2002, Ko-Kang Kevin Wang wrote:

    Hi,

    This is a simple question with if elseif....however I am having trouble
    constructing the solution for some reason.

    Suppose I have a data set with 3 variables, a, b and c say. Let's say c
    is the sum of a and b. So:
    a b c
    1 2 3
    2 3 5
    3 4 7
    . . .
    . . .
    . . .

    Suppose that I know there have been some data entry errors and I want to
    check if ALL values in c is really the sum of a and b, and if not, print
    out the whole line (i.e. all values of a, b and c in that row).

    Any help on how I can write this if elseif block will be apprecaited!
    Try subsetting:
    dataset <- data.frame(a, b, c)
    dataset[a + b != c, ]
    is what you want. You should consider whether you want to test exact or
    approximate equality, though.

    G?ran
    Ko-Kang

    ------------------------------------------------------------------------------
    Ko-Kang Kevin Wang
    Postgraduate PGDipSci Student
    Department of Statistics
    University of Auckland
    New Zealand

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
    --
    G?ran Brostr?m tel: +46 90 786 5223
    professor fax: +46 90 786 6614
    Department of Statistics http://www.stat.umu.se/egna/gb/
    Ume? University
    SE-90187 Ume?, Sweden e-mail: gb at stat.umu.se

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Ggrothendieck at Mar 24, 2002 at 2:26 pm
    d <- data.frame( a = 1:3, b = 2:4, c=c(3,5,9) ) # last row wrong
    subset(d, a+b!=c )
    On 24 Mar 2002 at 22:41, Ko-Kang Kevin Wang wrote:

    Hi,

    This is a simple question with if elseif....however I am having trouble
    constructing the solution for some reason.

    Suppose I have a data set with 3 variables, a, b and c say. Let's say c
    is the sum of a and b. So:
    a b c
    1 2 3
    2 3 5
    3 4 7
    . . .
    . . .
    . . .

    Suppose that I know there have been some data entry errors and I want to
    check if ALL values in c is really the sum of a and b, and if not, print
    out the whole line (i.e. all values of a, b and c in that row).

    Any help on how I can write this if elseif block will be apprecaited!

    Ko-Kang

    ------------------------------------------------------------------------------
    Ko-Kang Kevin Wang
    Postgraduate PGDipSci Student
    Department of Statistics
    University of Auckland
    New Zealand

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
    Send "info", "help", or "[un]subscribe"
    (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedMar 24, '02 at 10:41a
activeMar 25, '02 at 10:04a
posts5
users5
websiter-project.org
irc#r

People

Translate

site design / logo © 2017 Grokbase