Re: [R] How to check to see if a variable is within a range of another variable
Is there an easy way to check whether a variable is within +/- 10% range of another variable in R? You could use 2*abs(A-B)/(A+B) 0.1 which avoids an apply(). I've assumed you meant different by under 10% of the mean of the two, hence the 2/(A+B); if you meant 10% of something else, same kind of thing should work with a different bottom line. For example if you meant less than 10% of the smaller of the two, abs(A-B)/pmin(A, B) 0.1 would do that And if you want to check that they all comply, all( 2*abs(A-B)/(A+B) 0.1 ) will do that. I liked Peter Langfelder's suggestion of all.equal, though; an interesting use of a tolerance I've previously seen as there only to compensate for floating point representation errors. Steve E *** This email and any attachments are confidential. Any use...{{dropped:8}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to check to see if a variable is within a range of another variable
On 01/10/2014 23:54, Peter Alspach wrote: Tena koe Kate If kateDF is a data.frame with your data, then apply(kateDF, 1, function(x) isTRUE(all.equal(x[2], x[1], check.attributes = FALSE, tolerance=0.1))) comes close to (what I think) you want (but not to what you have illustrated in your 'eventual outcome'). Anyhow, it may be enough to allow you to get there. HTH Peter Alspach I seem to need to specify all.equal(..., scale) to get correct values for some data/tolerances: -- aDF - data.frame(a = 1:10, b=10:1) apply(aDF, 1, function(x) isTRUE(all.equal(x[2], x[1], check.attributes = FALSE, tolerance = 5, scale = 1)) ) # [1] FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE apply(aDF, 1, function(x) isTRUE(all.equal(x[2], x[1], check.attributes = FALSE, tolerance = 5)) ) # [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE -- I'm probably being stupid, but from reading ?all.equal I would have expected scale = 1 and the default scale = NULL to give identical results for the length one numerics being passed to all.equal. Can anyone explain? KJ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to check to see if a variable is within a range of another variable
Keith Jewell said: ... from reading ?all.equal I would have expected scale = 1 and the default scale = NULL to give identical results for the length one numerics being passed to all.equal. Can anyone explain? Inspectng the code in all.equal.numeric, I find xy - mean((if (cplx) Mod else abs)(target - current)) if (is.null(scale)) { xn - mean(abs(target)) if (is.finite(xn) xn tolerance) { xy - xy/xn relative } else absolute } else { xy - xy/scale if (scale == 1) absolute else scaled } target is the first number supplied, current the second; in yoour example code that is x[2] and x[1] respectively. Later on xy is compared to the tolerance. In the code, scale=NULL and scale=1 are clearly treated differently; in particular when scale is NULL the absolute difference is divided by first of the (two) numbers if that number is greater than tolerance or is used unchanged, and if scale=1 it is divided by scale throughout. That would mean that for scale=NULL, your example will divide the difference by 10, 9, ..1 in that order before comparing with tolerance, and if scale=1 it will simply compare the difference directly with the tolerance. Calculating your case through for scale = NULL, xy will take the values ifelse(b5, abs(a-b)/b, abs(a-b)) [1] 0.900 0.778 0.625 0.4285714 0.167 1.000 3.000 [8] 5.000 7.000 9.000 Of those, only the last 2 are greater than 5, which is the result you found. By contrast, when scale=1 xy takes the values abs(a-b) [1] 9 7 5 3 1 1 3 5 7 9 of which the two at each end are both greater than 5. That fairly complicated behavio0ur is probably a good reason to use a simpler calculation in which you can see how the difference is being scaled ... ;) Steve Ellison *** This email and any attachments are confidential. Any use...{{dropped:8}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to check to see if a variable is within a range of another variable
Is there an easy way to check whether a variable is within +/- 10% range of another variable in R? Say, if I have a variable 'A', whether its in +/- 10% range of variable 'B' and if so, create another variable 'C' to say whether it is or not? Is there a function that is able to do that? eventual outcome: A B C 67 76 no 24 23 yes 40 45 yes 10 12 yes 70 72 yes 101 90 no 9 12 no __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to check to see if a variable is within a range of another variable
On Wed, Oct 1, 2014 at 3:11 PM, Kate Ignatius kate.ignat...@gmail.com wrote: Is there an easy way to check whether a variable is within +/- 10% range of another variable in R? Yes, checkRange = function(A, B, range = 0.1) { A=B*(1-range) A=B*(1+range); } Test: A = c(67, 24, 40, 10, 70, 101, 9) B = c(76, 23, 45, 12, 72, 90, 12) outcome = checkRange(A, B) You can create the desired data frame for example as data.frame (A = A, B=B, C = c(no, yes)[outcome+1]) Say, if I have a variable 'A', whether its in +/- 10% range of variable 'B' and if so, create another variable 'C' to say whether it is or not? What do you mean by range of variable B? In your example below, 40 is not within 10% of 45, which is 4.5; 10 is not within 10% of 12 which is 1.2. eventual outcome: A B C 67 76 no 24 23 yes 40 45 yes 10 12 yes 70 72 yes 101 90 no 9 12 no HTH, Peter __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to check to see if a variable is within a range of another variable
Tena koe Kate If kateDF is a data.frame with your data, then apply(kateDF, 1, function(x) isTRUE(all.equal(x[2], x[1], check.attributes = FALSE, tolerance=0.1))) comes close to (what I think) you want (but not to what you have illustrated in your 'eventual outcome'). Anyhow, it may be enough to allow you to get there. HTH Peter Alspach -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Kate Ignatius Sent: Thursday, 2 October 2014 11:11 a.m. To: r-help Subject: [R] How to check to see if a variable is within a range of another variable Is there an easy way to check whether a variable is within +/- 10% range of another variable in R? Say, if I have a variable 'A', whether its in +/- 10% range of variable 'B' and if so, create another variable 'C' to say whether it is or not? Is there a function that is able to do that? eventual outcome: A B C 67 76 no 24 23 yes 40 45 yes 10 12 yes 70 72 yes 101 90 no 9 12 no __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. The contents of this e-mail are confidential and may be ...{{dropped:14}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to check to see if a variable is within a range of another variable
Apologise - yes, my 10% calculations seem to be slightly off. However, the function gives me all falses which seems to be a little weird. Even where both columns equal each other. Should that be right? In essence I want to check whether A and B equal other give or take 10%. On Wed, Oct 1, 2014 at 6:54 PM, Peter Alspach peter.alsp...@plantandfood.co.nz wrote: Tena koe Kate If kateDF is a data.frame with your data, then apply(kateDF, 1, function(x) isTRUE(all.equal(x[2], x[1], check.attributes = FALSE, tolerance=0.1))) comes close to (what I think) you want (but not to what you have illustrated in your 'eventual outcome'). Anyhow, it may be enough to allow you to get there. HTH Peter Alspach -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Kate Ignatius Sent: Thursday, 2 October 2014 11:11 a.m. To: r-help Subject: [R] How to check to see if a variable is within a range of another variable Is there an easy way to check whether a variable is within +/- 10% range of another variable in R? Say, if I have a variable 'A', whether its in +/- 10% range of variable 'B' and if so, create another variable 'C' to say whether it is or not? Is there a function that is able to do that? eventual outcome: A B C 67 76 no 24 23 yes 40 45 yes 10 12 yes 70 72 yes 101 90 no 9 12 no __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to check to see if a variable is within a range of another variable
Tena koe Kate This is what I get: kateDF # A B # 1 67 76 # 2 24 23 # 3 40 45 # 4 10 12 # 5 70 72 # 6 101 90 # 7 9 12 kateDF$C - apply(kateDF, 1, function(x) isTRUE(all.equal(x[2], x[1], check.attributes = FALSE, tolerance=0.1))) kateDF # A B C # 1 67 76 FALSE # 2 24 23 TRUE # 3 40 45 FALSE # 4 10 12 FALSE # 5 70 72 TRUE # 6 101 90 FALSE # 7 9 12 FALSE Which seems correct. Can't say why you get something different without more details. Peter Alspach -Original Message- From: Kate Ignatius [mailto:kate.ignat...@gmail.com] Sent: Thursday, 2 October 2014 12:39 p.m. To: Peter Alspach Cc: r-help Subject: Re: [R] How to check to see if a variable is within a range of another variable Apologise - yes, my 10% calculations seem to be slightly off. However, the function gives me all falses which seems to be a little weird. Even where both columns equal each other. Should that be right? In essence I want to check whether A and B equal other give or take 10%. On Wed, Oct 1, 2014 at 6:54 PM, Peter Alspach peter.alsp...@plantandfood.co.nz wrote: Tena koe Kate If kateDF is a data.frame with your data, then apply(kateDF, 1, function(x) isTRUE(all.equal(x[2], x[1], check.attributes = FALSE, tolerance=0.1))) comes close to (what I think) you want (but not to what you have illustrated in your 'eventual outcome'). Anyhow, it may be enough to allow you to get there. HTH Peter Alspach -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Kate Ignatius Sent: Thursday, 2 October 2014 11:11 a.m. To: r-help Subject: [R] How to check to see if a variable is within a range of another variable Is there an easy way to check whether a variable is within +/- 10% range of another variable in R? Say, if I have a variable 'A', whether its in +/- 10% range of variable 'B' and if so, create another variable 'C' to say whether it is or not? Is there a function that is able to do that? eventual outcome: A B C 67 76 no 24 23 yes 40 45 yes 10 12 yes 70 72 yes 101 90 no 9 12 no __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited. The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.