## Type comparison
alike(1L, 1.0) # TRUE, because 1.0 is integer-like
alike(1L, 1.1) # FALSE, 1.1 is not integer-like
alike(1.1, 1L) # TRUE, by default, integers are always considered real
alike(1:100, 1:100 + 0.0) # TRUE
## We do not check numerics for integerness if longer than 100
alike(1:101, 1:101 + 0.0)
## Scalarness can now be checked at same time as type
alike(integer(1L), 1) # integer-like and length 1?
alike(logical(1L), TRUE) # logical and length 1?
alike(integer(1L), 1:3)
alike(logical(1L), c(TRUE, TRUE))
## Zero length match any length of same type
alike(integer(), 1:10)
alike(1:10, integer()) # but not the other way around
## Recursive objects compared recursively
alike(
list(integer(), list(character(), logical(1L))),
list(1:10, list(letters, TRUE))
)
alike(
list(integer(), list(character(), logical(1L))),
list(1:10, list(letters, c(TRUE, FALSE)))
)
## `NULL` is a wild card when nested within recursive objects
alike(list(NULL, NULL), list(iris, mtcars))
alike(NULL, mtcars) # but not at top level
## Since `data.frame` are lists, we can compare them recursively:
iris.fake <- transform(iris, Species=as.character(Species))
alike(iris, iris.fake)
## we even check attributes (factor levels must match)!
iris.fake2 <- iris
levels(iris.fake2$Species) <- c("setosa", "versicolor", "africana")
alike(iris, iris.fake2)
## We can use partially specified objects as templates
iris.tpl <- abstract(iris)
str(iris.tpl)
alike(iris.tpl, iris)
## any row sample of iris matches our iris template
alike(iris.tpl, iris[sample(1:nrow(iris), 10), ])
## but column order matters
alike(iris.tpl, iris[c(2, 1, 3, 4, 5)])
## 3 x 3 integer
alike(matrix(integer(), 3, 3), matrix(1:9, nrow=3))
## 3 x 3, but not integer!
alike(matrix(integer(), 3, 3), matrix(runif(9), nrow=3))
## partial spec, any 3 row integer matrix
alike(matrix(integer(), 3), matrix(1:12, nrow=3))
alike(matrix(integer(), 3), matrix(1:12, nrow=4))
## Any logical matrix (but not arrays)
alike(matrix(logical()), array(rep(TRUE, 8), rep(2, 3)))
## In order for objects to be alike, they must share a family
## tree, not just a common class
obj.tpl <- structure(TRUE, class=letters[1:3])
obj.cur.1 <- structure(TRUE, class=c("x", letters[1:3]))
obj.cur.2 <- structure(TRUE, class=c(letters[1:3], "x"))
alike(obj.tpl, obj.cur.1)
alike(obj.tpl, obj.cur.2)
## You can compare language objects; these are alike if they are self
## consistent; we don't care what the symbols are, so long as they are used
## consistently across target and current:
## TRUE, symbols are consistent (adding two different symbols)
alike(quote(x + y), quote(a + b))
## FALSE, different function
alike(quote(x + y), quote(a - b))
## FALSE, inconsistent symbols
alike(quote(x + y), quote(a + a))
Run the code above in your browser using DataLab