3 ms·
It's not WTF at all once you actually understand why it works that way and the benefits it provides. Subsetting in R allows for any index number to be retrieved
by txcwpalpha 8y ago
It's not WTF at all once you actually understand why it works that way and the benefits it provides. Subsetting in R allows for any index number to be retrieved, and if there is no value, it returns an empty value (that's what "numeric(0)" is: an empty numeric value). It's the same if you tried to access a[0] or a[90000] (in an array that doesn't have 90000 elements). This makes it easier to select multiple elements at the same time or select a range of elements; if you try to access one that doesn't exist, it just returns an empty value rather than erroring.
You can also access values by negative indexes, which have another special purpose in R that make it easier to quickly manipulate/analyze the data in the array, rather than causing an error.
It's just like how SQL won't throw an error if you try to do a SELECT...WHERE ID=non_existant_value, instead it will just not return any rows (or return NULL, depending on the exact query). But you wouldn't call SQL bad, you would just acknowledge that it serves different purposes and acts differently than something like Java or C.
Your comment is a great example of what the parent commenter was talking about: experienced "programmers" tend to dislike R simply because they are unfamiliar with it, and it therefor does things that they do not expect. But again, that doesn't mean the language is bad, it just means the users should probably become more familiar with their tools rather than trying to use a wrench to bang on a nail.
- vertere 8y ago> It's the same if you tried to access a[0] or a[90000] (in an array that doesn't have 90000 elements). It's not the same: `a[90000]` would be `NA` (which is a 1-element vector, not an empty vector).
- jandrese 8y agoIt has a different behavior when you access past the end of the array. That makes error checking more difficult since you have to test for both error conditions. 1 based indexing is a mistake, but compounding that by changing the error conditions is the WTF. Having 1 based indexing is a design flaw that R shares with SQL. SQL's error handling also leaves much to be desired.