To understand what is going on, you must understand the structure of data frames and disposal rules. A data frame is just a list of vectors.
> unclass(df) $A [1] 10 20 10 10 20 30 $B [1] 50 60 50 40 70 80 attr(,"row.names") [1] 1 2 3 4 5 6
If you are comparing two vectors of different lengths in R, the shorter one is recycled . In your case, df$A == c(10,20) equivalent to:
> c(10, 20, 10, 10, 20, 30) == c(10, 20, 10, 20, 10, 20) [1] TRUE TRUE TRUE FALSE FALSE FALSE
and
> df[c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE), ] AB 1 10 50 2 20 60 3 10 50
From %in% documentation :
%in% returns a logical vector indicating whether there is a match or not for its left operand
> df$A %in% c(10,20) [1] TRUE TRUE TRUE TRUE TRUE FALSE
and
> df[c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE), ] AB 1 10 50 2 20 60 3 10 50 4 10 40 5 20 70
zero323
source share