I have three vectors
evens <- c(2,4,6,8,10,12,14,16,18,20)
r <- c(4,5,8,9,12)
t <- c(10,12)
which( evens > r)# returns to 6,7,8,9,10
which( evens > t)# returns to 7,8,9,10
Why are the results different?
Could you please explain how which function works?
As others have already pointed out this is due to the recycling rules of R. You can see how it works in detail by doing this:
> data.frame(evens, r, t, evens > r, evens > t)
evens r t evens...r evens...t
1 2 4 10 FALSE FALSE
2 4 5 12 FALSE FALSE
3 6 8 10 FALSE FALSE
4 8 9 12 FALSE FALSE
5 10 12 10 FALSE FALSE
6 12 4 12 TRUE FALSE
7 14 5 10 TRUE TRUE
8 16 8 12 TRUE TRUE
9 18 9 10 TRUE TRUE
10 20 12 12 TRUE TRUE
So when you do which(evens > r)
you get the indices of all TRUE
s in the column event...r
above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.