user3036416
user3036416

Reputation: 1255

Subset a dataframe according to multiple negative condition

I would like to subset a dataframe

> head(SH)

  V11       yr  mo dy
1 US009239 1941  2 14
2 US009239 1941  2 14
3 US009239 1941  2 14
4 US009239 1941  2 15
5 US009239 1941  2 15
6 US009239 1941  2 15

in this way

test <- subset(SH,SH$yr!=yrmin & SH$mo !=momin)

where

> unique(SH$yr)
[1] 1941 1940

> unique(SH$mo)
[1]  2  1 12

momin =2
yrmin =1941

Anyway, what I get is

unique(test$yr)
[1] 1940

> unique(test$mo)
[1] 12

meaning that the subset function does not consider together the 2 conditions.

Many thanks

Upvotes: 0

Views: 1132

Answers (1)

Mullefa
Mullefa

Reputation: 1247

If you want to combine logical statements with boolean operators, and don't want to use standard evaluation i.e. [, then I suggest looking at the new package by Hadley dplyr e.g.

library(dplyr)

filter(iris, Sepal.Length != 4 & Sepal.Width != 4)

# or equivalently
filter(iris, Sepal.Length != 4, Sepal.Width != 4)

In fact, I suggest looking at dplyr anyway: it's awesome!

Upvotes: 1

Related Questions