Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subset function with "different than"?

Tags:

dataframe

r

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

like image 471
Katerina Avatar asked Aug 30 '11 15:08

Katerina


3 Answers

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.

like image 131
James Avatar answered Sep 29 '22 12:09

James


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
like image 29
Andrie Avatar answered Sep 29 '22 12:09

Andrie


Using the %nin% function in Hmisc

require(Hmisc)
subset(dataset, IA_LABEL %nin% wordList)
like image 27
Ramnath Avatar answered Sep 29 '22 14:09

Ramnath