[R] How to check to see if a variable is within a range of another variable
Peter Alspach
Peter.Alspach at plantandfood.co.nz
Thu Oct 2 01:45:52 CEST 2014
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.ignatius at 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.Alspach at 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-bounces at r-project.org
> [mailto:r-help-bounces at 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 at 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.
More information about the R-help
mailing list