dframeEquiv: Examines the equivalence of two dataframes or matrices

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/dframeEquiv.R

Description

Checks whether two data objects (data frames and/or matrices) are equivalent and returns a descriptive message describing the result.

Usage

1
2
dframeEquiv(d1, d2, maxAbsError = 1e-12, maxRelError = 1e-14,
  verbose = TRUE)

Arguments

d1

The first dataframe or matrix

d2

The dataframe or matrix that will be compared to d1

maxAbsError

Numeric values whose absolute difference is less than maxAbsError will be declared equivalent

maxRelError

Numeric values whose relative difference is within maxRelError will be declared equivalent

verbose

=TRUE prints the result of the comparison

Details

d1 and d2 do not both have to be of the same mode; i.e. d1 could be a dataframe and d2 could be a matrix. If the number of rows or the number of columns differ, then no further comparisons are made. If the colnames or rownames differ, then those differences are noted and comparison continues. If two corresponding elements are both NA, then they are considered equivalent. Likewise, Inf is considered equivalent to Inf and -Inf is considered equivalent to -Inf. Factors in dataframes are converted to character strings prior to comparison. Comparisons are made one column at a time.

If a particular column from both objects are numeric, then for two corresponding values, say, a and b, equivalence is declared if one or more of the following occurs: 1) a == b, 2) abs(a - b) < maxAbsError, 3) abs((a - b) / b) < maxRelError if abs(b) > abs(a), or abs((a - b) / a) < maxRelError if abs(b) >= abs(a).

If both columns are not numeric, they are coerced (if need be) to character and then compared directly.

Value

Invisibly returns a list with the following components. (If the matrices do not have the same dimensions or the same colnames and rownames, then frac.equiv, loc.equiv, and equiv.matrix are all NULL).

equiv

=TRUE if d1 is equivalent to d2

msg

Messages that describe the comparison. (These are printed when verbose=TRUE.)

frac.equiv

The fraction of matrix elements that are equivalent

loc.inequiv

A data frame indicating the row and column coordinate locations of the elements that are not equivalent

eqiv.matrix

A boolean matrix with the same dimension as d1 and d2, indicating the equivalent elements

Author(s)

Landon Sego

References

https://randomascii.wordpress.com/category/floating-point

See Also

all.equal, identical

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Number of rows different
dframeEquiv(matrix(rnorm(20), nrow = 4),
            matrix(rnorm(25), nrow = 5))

# Number of columns different
dframeEquiv(matrix(rnorm(16), nrow = 4),
            matrix(rnorm(20), nrow = 4))

# Rownames differ
dframeEquiv(matrix(rnorm(9), nrow = 3, dimnames = list(1:3, NULL)),
            matrix(rnorm(9), nrow = 3, dimnames = list(letters[1:3], NULL)))

# Colnames differ
dframeEquiv(matrix(rnorm(9), nrow = 3, dimnames = list(NULL, 1:3)),
            matrix(rnorm(9), nrow = 3, dimnames = list(NULL, letters[1:3])))

# Not equivalent
x <- data.frame(x = factor(c(1,1,2,2,3,3)), y = rnorm(6))
y <- data.frame(x = factor(c(1,2,2,2,3,3)), y = c(x$y[-6],rnorm(1)))
dframeEquiv(x, y)

# Look at discrepancies
out <- dframeEquiv(x, y)
out

# Equivalent
x <- data.frame(x = letters[1:6], y = 0:5)
y <- x
dframeEquiv(x, y)

Smisc documentation built on May 2, 2019, 2:46 a.m.