Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing values of two tables with respect to a tolerance in R

I wanted to compare two tables. If the value of table 2 placed in 0.3 tolerance (+0.3 and -0.3) of the table 2 call it normal otherwise call it abnormal.

Sample Data:

Table 1.                        

0.17666667
-0.2413333
-0.179666
0.182437
0.012229
0.127333
-0.1180
0.8873
1.24100
1.5213

Table 2.

-1.6
-0.5
-0.4
-0.4
-0.2
2.5
0.6
2.2
2.3
1.3

Expected results for first row:

if 0.17666667**-0.3**<-1.6<0.17666667**+0.3**

result NORMAL Otherwise Abnormal

like image 546
A.Amidi Avatar asked Dec 28 '12 13:12

A.Amidi


People also ask

How do I check if two sets of data are the same in R?

Example 1: Check Whether Two Data Frames are Equal Using identical() Function. The RStudio console returns the logical value TRUE, i.e. our two data frames data1 and data2 are the same. This time, the RStudio console prints the logical value FALSE, i.e. data1 and data3 are not the same.

How do you compare two data tables?

Compare two tables by using joins. To compare two tables by using joins, you create a select query that includes both tables. If there is not already an existing relationship between the tables on the fields that contain the corresponding data, you create a join on the fields that you want to examine for matches.


1 Answers

You could also do this with all.equal.

table.1 <- scan(text="
0.17666667
-0.2413333
-0.179666
0.182437
0.012229
0.127333
-0.1180
0.8873
1.24100
1.5213")

table.2 <- scan(text="
-1.6
-0.5
-0.4
-0.4
-0.2
2.5
0.6
2.2
2.3
1.3")

are.close <- function(x, y, tol) isTRUE(all.equal(x, y, tol))
close <- mapply(are.close, x=table.1, y=table.2, tol=0.3)
result <- ifelse(close, 'N', 'A')
# [1] "A" "N" "N" "A" "N" "A" "A" "A" "A" "N"
like image 80
Matthew Plourde Avatar answered Sep 29 '22 07:09

Matthew Plourde