Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing equality of two functions in R [duplicate]

Tags:

r

Is there a way to determine if the text of two different functions is identical?

x <- function(x) print(x + 2)
y <- function(x) print(x + 2)
identical(x, y)
[1] FALSE
identical(mget("x"), mget("y"))
[1] FALSE
identical(unname(mget("x")), unname(mget("y")))
[1] FALSE
like image 306
Kevin Burnham Avatar asked Sep 16 '16 18:09

Kevin Burnham


People also ask

How do you check if two sets are equal in R?

setequal() function in R Language is used to check if two objects are equal. This function takes two objects like Vectors, dataframes, etc. as arguments and results in TRUE or FALSE, if the Objects are equal or not.

What does identical () do in R?

identical() function in R Language is used to return TRUE when two objects are equal else return FALSE.

What is all equal in R?

all. equal(x, y) is a utility to compare R objects x and y testing 'near equality'. If they are different, comparison is still made to some extent, and a report of the differences is returned.


2 Answers

I think this is a good method. It works for many different objects:

all.equal(x,y)
[1] TRUE
like image 192
Pierre L Avatar answered Oct 13 '22 00:10

Pierre L


Using diffobj package:

library(diffobj)

x <- function(x) print(x + 2)
y <- function(x) print(x + 2)

diffPrint(target = x, current = y)

enter image description here

Wrapping it in any() will give TRUE/FALSE:

any(diffPrint(target = x, current = y))
# FALSE
like image 24
zx8754 Avatar answered Oct 13 '22 01:10

zx8754