I want to identify which values in one vector are present in another vector. Sometimes, in my application, none of the values of the first vector are present; in such cases I would like NA. My current approach returns integer(0) when this occurs:
l <- 1:3
m <- 2:5
n <- 4:6
l[l %in% m]
1] 2 3
l[l %in% n]
integer(0)
This post discusses how to capture integer(0) using length
, but is there a way to avoid integer(0) in the first place, and do this operation in just one step? Answers to the previous question suggest that any
could be used but I fail to see how that would work in this example.