match
match는 2nd argument와 일치하는 1st argument의 위치값vector를 리턴
%in%은 T/F값을 리턴
## The intersection of two sets can be defined via match(): ## Simple version: 5:10 %in% c(7:15) # [1] FALSE FALSE TRUE TRUE TRUE TRUE match(5:10, 7:15) # [1] NA NA 1 2 3 4 match(7:15, 5:10) # [1] 3 4 5 6 NA NA NA NA NA ## intersect <- function(x, y) y[match(x, y, nomatch = 0)] intersect(5:10, 7:15) # [1] 7 8 9 10
sstr <- c("c","ab","B","bba","c",NA,"@","bla","a","Ba","%") alphabet <- c(letters, LETTERS) sstr[sstr %in% alphabet ] # [1] "c" "B" "c" "a" sstr[which(sstr %in% alphabet)] # [1] "c" "B" "c" "a" alphabet[ match(sstr, alphabet, nomatch=0) ] # [1] "c" "B" "c" "a" sstr[match(alphabet, sstr, nomatch=0)] # [1] "a" "c" "B"
"%w/o%" <- function(x, y) x[!x %in% y] #-- x without y (1:5) %w/o% c(2,3,12) # [1] 1 4 5 ## Note that setdiff() is very similar and typically makes more sense: c(1:6,7:2) %w/o% c(3,7,12) # -> keeps duplicates setdiff(c(1:6,7:2), c(3,7,12)) # -> unique values