In Python there is a nice way to index that would have obvious conflicts in R. This is the negative indexing. In Python, I can say something like
someList[-2] to access the second to last element of the list, or to grab the last two elements I could say
someList[-2:]. This is like sequence indexing in R on a vector:
someVector[2:4]. In Python, though, the empty part of the sequence implies "to the end." In R, you have to be explicit
Code:
someList[2:] # Python
someVector[2:length(someVector)] # R
In R, negatives are treated as "exclude" indicators. Thus,
someVector[-2] would mean include all values
except 2. In Python, as explained, it would grab that distance from the end of the vector.
What I learned today is that
head has some nice properties that can mimic this Python behavior! I know that you can do something like
Code:
head(x, 20) # Reveals 20 elements instead of default 5
What I did not realize is that the number can be a negative amount to exclude from the end.
Code:
head(x, -5) # All but last 5 elements
Be creative. There are uses for this!