Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we talk about reference type and primitive type in R?

Tags:

r

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.

like image 771
J-A-S Avatar asked Oct 31 '20 10:10

J-A-S


Video Answer


1 Answers

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.

like image 139
RHertel Avatar answered Sep 24 '22 20:09

RHertel