Last week I tried to translate a C# program to R. It was so painful with the indexes. A graph with 100 nodes , a few vectors and pain.
This somewhat relates to another feature that I consider an annoyance: vector recycling. I am OK with the existence of recycling, but not with using it as the default. I can track quite a few of my own mistakes to that feature.
Couldn't agree more. Vector recycling is a senseless use that pays no heed to the veracity of the arithmetic operation and I have no reason to think anyone requested it as a feature!
Maybe a way to address it while maintaining the R engine backward compatible is to raise a warning when 0s in indexing.
the interpreter will stop on warnings, giving a safer computational framework. What do you think about that?
Those are some cool "features". The last one apparently happens because NA is logical while NA+0 is numeric.
but I still don't like the default.
This is just to support your statement that it is not clear what to do with subsetting with NAs.
Is there a way to preview submissions?
Unfortunately, there seems to be no way to edit comments after they're submitted, even for me when moderating them.
I'm reminded of the useful conversational elements in the English-Spanish phrase book.
Does anyone here speak Norwegian?
Using = rather than <- does avoid the formatting problems.
The effect of 4 versus -4 does seem a bit funny, though individually the definitions of what they do seem reasonably sensible (not necessarily the best way, since many buggy references will be considered valid, but not silly).
Many properties that one might hope for aren't going to be satisfied by R's indexing. For instance, one might hope that saving the i'th element in another variable, then setting it to something else, then setting it back to its saved value, would leave the vector unchanged. But of course it won't if the index is beyond the previous extent of the vector.
# the first element might well be 3, were it not missing!!!!
I agree that it's not clear that an error should always be signalled when using NA in a subscript to access elements. I say above only that it should be an error when replacing elements.
However, I'm not convinced that the example you give is good behaviour. If I use a vector of TRUE/FALSE values as a subscript, I expect to get a vector whose length is equal to the number of TRUE values. If some of these TRUE/FALSE values are actually NA, the length of vector to return is uncertain (if you think of NA as being unceretainly either TRUE or FALSE). This is not at all the same as a vector of a well-defined length in which some elements are NA. In particular, it's unlikely that the presence of the NA elements will correctly propagate through subsequent operations to produce a sensible result. So it may be best to signal an error when a logical vector containing an NA is used as a subscript.
The situation is different when the subscript is a vector of integers, some of which are NA. Then the result should be a vector the same length as the subscript, and putting in NA where the subscript is NA correctly represents the uncertainty in the value of that subscript.
It's interesting that %in% handles NA the way you show, though I'm not sure it's consistent with R's general treatment of NA!
yes, R is a funny language. it does have its own logic, though!
But the feature works so well with your other favourite feature!
RN: I edited this to restore what I take to be what was intended. Remember! To enter a "<" character, you need to use "&lt;".