Learn to compare objects in R using built-in comparison functions. This tutorial is part of the Working With Data module of the R Programming course offered by r-squared.
3. r-squared
Slide 3
Working With Data
www.r-squared.in/rprogramming
✓ Data Types
✓ Data Structures
✓ Data Creation
✓ Data Info
✓ Data Subsetting
✓ Comparing R Objects
✓ Importing Data
✓ Exporting Data
✓ Data Transformation
✓ Numeric Functions
✓ String Functions
✓ Mathematical Functions
4. r-squared
Slide 4
Comparing R Objects
www.r-squared.in/rprogramming
In this unit, we will explore built-in R function that can be used for comparing R objects:
● all.equal()
● all()
● any()
● stopifnot()
● duplicated()
● anyduplicated()
● identical()
● Comparison Operators
● Relational operators
6. r-squared
Slide 6
all.equal()
www.r-squared.in/rprogramming
Examples
> example 1
> x <- 5
> y <- 5.000000000000000001
> all.equal(x, y)
[1] TRUE
> example 2
> x <- 3
> y <- 3.1
> all.equal(x, y)
[1] "Mean relative difference: 0.03333333"
> example 3
> x <- 5
> y <- 15
> all.equal(x, y)
[1] "Mean relative difference: 2"
7. r-squared
Slide 7
all()
www.r-squared.in/rprogramming
Description:
all() checks if all the values in a given vector satisfy the supplied condition.
Syntax:
all(logical expression)
Returns:
all returns either TRUE or FALSE. If all the values in the vector satisfy the logical
expression, it returns TRUE but even if one of the values do not meet the logical
expression, it returns FALSE.
Documentation
help(all)
8. r-squared
Slide 8
all()
www.r-squared.in/rprogramming
Examples
> example 1
> x <- 1:10
> x
[1] 1 2 3 4 5 6 7 8 9 10
> all(x > 5)
[1] FALSE
# x > 5 is the logical expression. Since all values in x are not greater than 5, all()
returns FALSE.
> example 2
> x <- 1:10
> all(x > 0)
[1] TRUE
# x > 0 is the logical expression. Since all values in x are greater than 0, all() returns
TRUE.
9. r-squared
Slide 9
any()
www.r-squared.in/rprogramming
Description:
any() checks if at least one value in a given vector meets the supplied condition.
Syntax:
any(logical expression)
Returns:
any returns either TRUE or FALSE. If at least one value in the vector meets the logical
expression, it returns TRUE but if none of the values meet the logical expression, it returns
FALSE.
Documentation
help(any)
10. r-squared
Slide 10
any()
www.r-squared.in/rprogramming
Examples
> example 1
> x <- 1:10
> any(x > 9)
[1] TRUE
# x > 9 is the logical expression. any() checks if at least one value in x is greater than 9,
since 10 > 9, it returns TRUE.
> example 2
> x <- 1:10
> any(x < 1)
[1] FALSE
# x < 1 is the logical expression. any() checks if at least one value in x is less than 1.
Since none of the values are less than 1, it returns FALSE.
11. r-squared
Slide 11
stopifnot()
www.r-squared.in/rprogramming
Description:
stopifnot() evaluates the arguments supplied only if a pre-condition is satisfied. It is
very useful in error handling where it checks for a the supplied condition before executing
statements in functions.
Syntax:
stopifnot(condition,...)
Returns:
stopifnot returns an error if the condition does not evaluate to TRUE, else it evaluates
the rest of the arguments. If the arguments evaluate to TRUE, it returns nothing else it
returns an error.
Documentation
help(stopifnot)
12. r-squared
Slide 12
stopifnot()
www.r-squared.in/rprogramming
Examples
> # example 1
> stopifnot(1 == 1, all.equal(pi, 3.14159265), 1 < 2)
# It checks if the first condition evaluates to TRUE, since it does, stopifnot evaluates the
rest of the arguments. Since the rest of the arguments evaluate to TRUE, it does not return
anything.
> # example 2
> stopifnot(1 == 1, all.equal(pi, 3.14159265), 1 > 2)
Error: 1 > 2 is not TRUE
# Since 1 > 2 evaluates to FALSE, stopifnot returns an error.
> # example 3
> stopifnot(1 == 2, all.equal(pi, 3.14159265), 1 < 2)
Error: 1 == 2 is not TRUE
# Since 1 == 2 evaluates to FALSE, stopifnot does not evaluate the rest of the arguments and
returns an error.
13. r-squared
Slide 13
duplicated()
www.r-squared.in/rprogramming
Description:
duplicated() tests if elements in a vector or data frame are duplicates of other elements
and returns a logical vector with boolean values for duplicated elements.
Syntax:
duplicated(vector/data frame)
Returns:
An vector of boolean values indicating whether an element is duplicated or not.
Documentation
help(duplicated)
21. r-squared
Slide 21
Comparison Operators
www.r-squared.in/rprogramming
R provides the following comparison operators:
Operator Name Example: x <- 5 Result
> greater than x > 5 FALSE
>= greater than or equal to x >= 5 TRUE
< less than x < 5 TRUE
<= less than or equal to x <= 5 FALSE
== equal to x == 5 TRUE
!= not equal to x != 5 FALSE
23. r-squared
Slide 23
Truth Table - !
www.r-squared.in/rprogramming
Truth table for the ! logical operator:
x !x Example: x <- 5 Result
True False x > 5
!(x > 5)
TRUE
FALSE
False True x < 5
!(x < 5)
FALSE
TRUE
24. r-squared
Slide 24
Truth Table - &
x y x & y Example:
x <- 5
y <- 8
Result
False False False (x > 5 & y > 10) FALSE
False True False (x > 5 & y < 8) FALSE
True False False (x < 5 & y > 10) TRUE
True True True (x < 5 & y < 8) FALSE
www.r-squared.in/rprogramming
Truth table for the & logical operator:
25. r-squared
Slide 25
Truth Table - |
www.r-squared.in/rprogramming
x y x | y Example:
x <- 5
y <- 8
Result
False False False (x > 5 | y > 8) False
False True True (x > 5 | y < 8) True
True False True (x < 5 | y > 8) True
True True True (x < 5 | y < 8) True
Truth table for the & logical operator:
27. r-squared
Slide 27
| Operator
www.r-squared.in/rprogramming
Examples
> # example 1
> x <- 5
> y <- 2
> (x > 4 | y > 4)
[1] TRUE
# Test if x > 4 or y > 4. Since one of the expression evaluates to TRUE, the result is TRUE.
> # example 2
> if (x > 4 | y > 4) {
+ cat("x or y is greater than 4")
+ } else {
+ cat("Neither x nor y is greater than 4.")
+ }
x or y is greater than 4
28. r-squared
Slide 28
& Operator
www.r-squared.in/rprogramming
Examples
> # example 1
> x <- 5
> y <- 2
> (x > 4 & y > 4)
[1] FALSE
# Test if x > 4 or y > 4. Since one of the expression evaluates to TRUE, the result is TRUE.
> if (x > 4 & y > 4) {
+ cat("x and y are greater than 4")
+ } else {
+ cat("Either x or y is not greater than 4.")
+ }
Neither x nor y is not greater than 4.
30. r-squared
Slide 30
Connect With Us
www.r-squared.in/rprogramming
Visit r-squared for tutorials
on:
● R Programming
● Business Analytics
● Data Visualization
● Web Applications
● Package Development
● Git & GitHub