-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comparing units using == #51
Comments
The first issue is by design; converting to SI may not be desirable in some cases; converting to the first argument seemed easiest. Of course, the user can do manual conversion before any mathematical or logical operations. For the second one, see R FAQ 7.31 -- it is not up to this package to change this. |
Dear Edzer, I see the problem, I think this would lead to unexpected results for some people. An alternative I could see is to throw a warning when unequal units are compared to suggest to do a explicit conversion before. If your interested I can create a merge request to that extent |
Are you suggesting that > set_units(1, m/s) == set_units(3.6, km/h)
[1] TRUE should always warn? |
It is of course a pity that > set_units(3.6, km/h) == set_units(1, m/s)
[1] FALSE but we have > all.equal(set_units(3.6, km/h), set_units(set_units(1, m/s), km/h))
[1] TRUE a way out would be adding an > all.equal(set_units(3.6, km/h), set_units(1, m/s))
[1] TRUE I guess that would be more in line with "the R way" of finding near-equality. |
I think > set_units(3.6, km/h) == set_units(1, m/s)
[1] FALSE
Warning message:
In '==' : Non equal units are compared, this might results in numerical inaccuracies due to conversions, consider either explicit conversion or the use of all.equal The problem with > a<-set_units(1:4,m/s)
> bb<-set_units(c(3.6,7.2, 10.8,14.4),km/h)
> mapply(all.equal, a,b)
[1] TRUE TRUE TRUE TRUE Maybe that is still the best possible solution |
> all.equal(a, bb)
[1] TRUE
> all.equal(bb, a)
[1] TRUE looks like |
The solution for comparing all elements like > mapply(units:::all.equal.units,split(a,1:length(a)),split(bb,1:length(bb)))
1 2 3 4
TRUE TRUE TRUE TRUE |
Ah, I see, you want to have a comparison for every element. Yes, something like that. |
I was just trying the units package and got some strange results when comparing to vectors of different units but the same quantity. Here I construct two vectors of the same speed but one in m/s while the other one is in km/h. Two things are there to notice, the order of arguments matters for the results and second even thought the speeds are the same "==" does not consider them the same. For the later issue it probably relates to numerical precision of comparison. An alternative would be to check if the values are the same within some precision limit but I'm not sure if that does not have other unintended consequences or produces unexpected results. An alternative would be to provide some methods that check for equality using some tolerance like all.equal as shown below.
For the order of the arguments I guess one unit is converted in the other one (second into first). An alternative would in case of two different units always convert into SI units so the order of the argument would at least not matter.
The text was updated successfully, but these errors were encountered: