You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: numbers/comparing_numeric_values.md
+16-14Lines changed: 16 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -8,11 +8,10 @@ There are multiple ways to compare numeric values and vectors. This includes [l
8
8
9
9
<br>
10
10
11
-
{#numeric_comparison}
12
-
## Comparison Operators
11
+
## Comparison Operators {#numeric_comparison}
13
12
The normal binary operators allow you to compare numeric values and provides the answer in logical form:
14
13
15
-
{linenos=off}
14
+
16
15
```r
17
16
x<y# is x less than y
18
17
x>y# is x greater than y
@@ -24,7 +23,7 @@ x != y # is x not equal to y
24
23
25
24
These operations can be used for single number comparison:
26
25
27
-
{linenos=off}
26
+
28
27
```r
29
28
x<-9
30
29
y<-10
@@ -35,7 +34,7 @@ x == y
35
34
36
35
and also for comparison of numbers within vectors:
37
36
38
-
{linenos=off}
37
+
39
38
```r
40
39
x<- c(1, 4, 9, 12)
41
40
y<- c(4, 4, 9, 13)
@@ -46,7 +45,7 @@ x == y
46
45
47
46
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:
48
47
49
-
{linenos=off}
48
+
50
49
```r
51
50
# How many pairwise equal values are in vectors x and y
52
51
sum(x==y)
@@ -55,18 +54,19 @@ sum(x == y)
55
54
56
55
If you need to identify the location of pairwise equalities in two vectors you can wrap the operation in the `which()` function:
57
56
58
-
{linenos=off}
57
+
59
58
```r
60
59
# Where are the pairwise equal values located in vectors x and y
61
60
which(x==y)
62
61
## [1] 2 3
63
62
```
64
63
65
-
{#numeric_exact}
66
-
### Exact Equality
64
+
<br>
65
+
66
+
## Exact Equality {#numeric_exact}
67
67
To test if two objects are exactly equal:
68
68
69
-
{linenos=off}
69
+
70
70
```r
71
71
x<- c(4, 4, 9, 12)
72
72
y<- c(4, 4, 9, 13)
@@ -75,7 +75,7 @@ identical(x, y)
75
75
## [1] FALSE
76
76
```
77
77
78
-
{linenos=off}
78
+
79
79
```r
80
80
x<- c(4, 4, 9, 12)
81
81
y<- c(4, 4, 9, 12)
@@ -84,10 +84,12 @@ identical(x, y)
84
84
## [1] TRUE
85
85
```
86
86
87
-
### Floating Point Comparison
87
+
<br>
88
+
89
+
## Floating Point Comparison {#numeric_near}
88
90
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.
89
91
90
-
{linenos=off}
92
+
91
93
```r
92
94
x<- c(4.00000005, 4.00000008)
93
95
y<- c(4.00000002, 4.00000006)
@@ -98,7 +100,7 @@ all.equal(x, y)
98
100
99
101
If the difference is greater than the tolerance level the function will return the mean relative difference:
0 commit comments