subset function with "different than"?

36,386

Solution 1

If you are wanting to exclude all of those words, then you are better of using a combination of the negation (NOT) operator, !, and set membership, %in%.

wordList <- c("Er","Sie","Es","wird","gleich")
subset(dataset, !(IA_LABEL %in% wordList))

To make it case insensitive you might want to wrap each of them in toupper or tolower.

Solution 2

The not equal operator is written !=

See ?Comparison for details.

An example using subset:

> subset(airquality, Day != 1, select = -Temp)[1:5, ]
  Ozone Solar.R Wind Month Day
2    36     118  8.0     5   2
3    12     149 12.6     5   3
4    18     313 11.5     5   4
5    NA      NA 14.3     5   5
6    28      NA 14.9     5   6

Solution 3

Using the %nin% function in Hmisc

require(Hmisc)
subset(dataset, IA_LABEL %nin% wordList)
Share:
36,386

Related videos on Youtube

Katerina
Author by

Katerina

Updated on August 31, 2020

Comments

  • Katerina
    Katerina over 3 years

    is it possible to use the subset function by saying sth like subset(dataset, IA_LABEL not equal to "Er" or "Sie" or "Es" or "wird" or "gleich") ? what interests me is the "not equal to" operator, is there something like that for the subset function?

    thanks, Katerina

  • Tommy
    Tommy over 12 years
    +1 for beating me to it by a minute or so... The parenthesis makes it clearer, but isn't strictly required. ! has lower precedence than %in%.