[R] function in R that's equivalent to SQL's "IN"
bcarvalh at jhsph.edu
Fri Oct 26 22:29:45 CEST 2007
z$y[z$x %in% w]
On Oct 26, 2007, at 4:19 PM, Em C wrote:
> Hi all,
> I'm trying to find
> something like the "==" operator that will work on vectors or
> equivalent to SQL's "IN" function. For e.g., if I have:
> x <- c(1,2,3,4,5)
> y <- c("apples", "oranges", "grapes", "bananas", "pears")
> z <- data.frame (x,y)
> w <- c(2,4,5)
> I want R to return the values "oranges", "bananas", "pears" through
> some function like this:
> z$y[z$x == w]
> or in sql speak, like this
> z$y[z$x IN w]
> To complicate matters, my x variable is a date variable (as is w).
> Essentially, I have a bunch of dates with associated values and I
> want to retrieve all the associated values for particular dates.
> Is there any R function that will do this in a one-line, non-messy
> way? Thanks very much for any help!
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help