Reputation: 1669
In R,
print((1:20)[-(15:18)])
prints the list of integers from 1 to 20 excepting 15 through 18, as expected.
But
print((1983:2010)[-(1992:1994)])
prints the list of integers from 1983 to 2010, including 1992 through 1994. The negative subsetting is not working.
What's going on here? I know R can have trouble with extremely large numbers, but I would not consider these numbers extremely large.
Upvotes: 1
Views: 1296
Reputation: 887971
This is based on the numeric index. In the second example, we don't have a vector of length > 1994, but in the former case, there is
length(1983:2010)
#[1] 28
The way to deal with this to use %in%
v1 <- 1983:2010
v1[!v1 %in% 1992:1994]
Or use the index based on the original length of the vector
v1[-((length(v1)-2):length(v1))]
Upvotes: 1
Reputation: 33822
The values in square brackets refer to the position of the number in the vector, not the number itself.
In your first example, numbers 15-18 also occupy the positions 15-18.
In your second example, numbers 1992-1994 don't occupy positions 1992-1994. They occupy positions 10-12. So you could use:
print((1983:2010)[-(10:12)])
Upvotes: 0