Skip to content

Commit bb656b4

Browse files
Update comparing_numeric_values.md
1 parent 8d51763 commit bb656b4

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

numbers/comparing_numeric_values.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ There are multiple ways to compare numeric values and vectors. This includes [l
88

99
<br>
1010

11-
{#numeric_comparison}
12-
## Comparison Operators
11+
## Comparison Operators {#numeric_comparison}
1312
The normal binary operators allow you to compare numeric values and provides the answer in logical form:
1413

15-
{linenos=off}
14+
1615
```r
1716
x < y # is x less than y
1817
x > y # is x greater than y
@@ -24,7 +23,7 @@ x != y # is x not equal to y
2423

2524
These operations can be used for single number comparison:
2625

27-
{linenos=off}
26+
2827
```r
2928
x <- 9
3029
y <- 10
@@ -35,7 +34,7 @@ x == y
3534

3635
and also for comparison of numbers within vectors:
3736

38-
{linenos=off}
37+
3938
```r
4039
x <- c(1, 4, 9, 12)
4140
y <- c(4, 4, 9, 13)
@@ -46,7 +45,7 @@ x == y
4645

4746
Note that logical values `TRUE` and `FALSE` equate to 1 and 0 respectively. So if you want to identify the number of equal values in two vectors you can wrap the operation in the `sum()` function:
4847

49-
{linenos=off}
48+
5049
```r
5150
# How many pairwise equal values are in vectors x and y
5251
sum(x == y)
@@ -55,18 +54,19 @@ sum(x == y)
5554

5655
If you need to identify the location of pairwise equalities in two vectors you can wrap the operation in the `which()` function:
5756

58-
{linenos=off}
57+
5958
```r
6059
# Where are the pairwise equal values located in vectors x and y
6160
which(x == y)
6261
## [1] 2 3
6362
```
6463

65-
{#numeric_exact}
66-
### Exact Equality
64+
<br>
65+
66+
## Exact Equality {#numeric_exact}
6767
To test if two objects are exactly equal:
6868

69-
{linenos=off}
69+
7070
```r
7171
x <- c(4, 4, 9, 12)
7272
y <- c(4, 4, 9, 13)
@@ -75,7 +75,7 @@ identical(x, y)
7575
## [1] FALSE
7676
```
7777

78-
{linenos=off}
78+
7979
```r
8080
x <- c(4, 4, 9, 12)
8181
y <- c(4, 4, 9, 12)
@@ -84,10 +84,12 @@ identical(x, y)
8484
## [1] TRUE
8585
```
8686

87-
### Floating Point Comparison
87+
<br>
88+
89+
## Floating Point Comparison {#numeric_near}
8890
Sometimes you wish to test for 'near equality'. The `all.equal()` function allows you to test for equality with a difference tolerance of 1.5e-8.
8991

90-
{linenos=off}
92+
9193
```r
9294
x <- c(4.00000005, 4.00000008)
9395
y <- c(4.00000002, 4.00000006)
@@ -98,7 +100,7 @@ all.equal(x, y)
98100

99101
If the difference is greater than the tolerance level the function will return the mean relative difference:
100102

101-
{linenos=off}
103+
102104
```r
103105
x <- c(4.005, 4.0008)
104106
y <- c(4.002, 4.0006)

0 commit comments

Comments
 (0)