When I studied java, I noticed that we avoid using ==
to compare reference types because ==
compares whether the references are the same, not the contents. And we would only use ==
for primitive types due to the way they're being stored in the memory.
I found a kinda similar note in R documentation for Relational Operators:
Do not use == and != for tests, such as in if expressions, where you must get a single TRUE or FALSE. Unless you are absolutely sure that nothing unusual can happen, you should use the identical function instead.
And immediately after this, I found:
For numerical and complex values, remember == and != do not allow for the finite representation of fractions, nor for rounding error. Using all.equal with identical is almost always preferable.
My humble questions:
1. Do we talk about primitive types in R? If so, what are they? And can we always safely use relational operators to compare them? (furthermore, under what situations could we be "absolutely sure that nothing unusual can happen" when using relational operators?)
2. I saw R codes comparing strings (characters) using ==
many times, are those R codes I saw just being sloppy or is that because character/string a primitive type in R (or something that we could always use relational operators to compare)?
[Updates]
Thanks for the comments below, I realized that the upper quote above is mainly trying to emphasize the vectorizing feature of R‘s operations rather than the accuracy of the output, and the validity of the relational operations in (base) R is highly unlikely to be affected by issues related to reference types.
Any answers or comments for further explanation/clarification are heartily welcomed.
References or pointers are not used in R. Therefore, a distinction between primitive types and reference types is not meaningful, and the pitfalls known, e.g., from Java or C++ of comparing a reference address instead of its content don't occur in R.
The warnings regarding the use of ==
for comparisons in R refer mainly to problems related to finite accuracy that can arise when checking floating-point values for equality, which can lead to counterintuitive results. Other possible mistakes connected with the use of the ==
operator in R concern the elementwise (rather than global) comparison of vectors, as was mentioned in the comments.
There are of course different "primitive" types (called data types or "atomic" modes) in R, such as numeric, logical, character, and complex. But it is usually not even necessary to specify the type of a variable because it is automatically deducted by the assignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With