.in. {Hmisc} | R Documentation

## Find Matching (or Non-Matching) Elements

### Description

For two vectors or scalars `a` and `b`, `a %in% b` returns a vector of logical values corresponding to the elements in `a`. A value is `TRUE` if that element of `a` is found somewhere in `b`, `FALSE` otherwise. If `a` is a factor object and `b` is numeric, converts `a` to its integer codes before the comparison. If `a` is numeric and `b` is `factor`, converts `b` to codes. `%nin%` returns `TRUE` if the element in `a` is not in `b`.

### Usage

    a %in% b
    a %nin% b

### Arguments

`a` | a vector (numeric, character, factor)
`b` | a vector (numeric, character, factor), matching the mode of a

### Value

vector of logical values with length equal to length of `a`.

### See Also

`match`

### Examples

    w <- factor(c("a","b","c"))
    w %in% c("b","c")
    w %in% c(2,3)      #same as previous, but with warning

    #Suppose that a variable x has levels "a", "b1", "b2" and you
    #want to classify levels "b1" and "b2" as just "b".  You can use
    #the %in% operator and do the following:
    # levels(x)[levels(x) %in% c("b1","b2")] <- "b"
    #Note that levels(x) <- list(b=c('b1','b2')) would be a better approach

[Package Contents]