0

I want to subset a data Frame. Most time one reduces an original data frame by keeping observations which fulfil certain conditions in its variables and dropping the rest.
A working code is:

Companies.Exchanges.1 <- subset(Companies.Exchanges.0,
                                (Frankfurt == 1 & London == 1))

I want to do it the other way round: Dropping all observations which fulfil certain conditions and keeping the rest - which violates at last one condition - in a new data frame.
How do I have to reformulate the above code to do this this?

4
  • Maybe using Frankfurt != 1 & London != 1 ? Commented Jul 11, 2014 at 20:31
  • This would delete all rows in my data set. Its more about finding all rows for which fulfill 'Location == 1' for all Locations (binary variables. In this example there is a maximum of two Locations:two) an then deleting them. Commented Jul 11, 2014 at 20:38
  • There should be only rows left who fulfill the condition that at least one Location equals 0 @Jilber Commented Jul 11, 2014 at 20:40
  • !(Frankfurt == 1 & London == 1) or equivalently Frankfurt != 1 | London != 1 Commented Jul 11, 2014 at 20:43

1 Answer 1

1

Try negating your filtering conditions with !

Companies.Exchanges.1 <- subset(Companies.Exchanges.0,
                            !(Frankfurt == 1 & London == 1))

When you specify filtering conditions for subset or in general, R takes all of your rows and checks them against the conditions you set. Think of it as adding another boolean vector to your dataframe where matching criteria = TRUE, and not matching = FALSE. The ! operator reverses this invisible vector.

Sign up to request clarification or add additional context in comments.

2 Comments

Are there any additional issues if the (binary) rows contain 1 and NA instead of 1 and 0? (unfortunaly this is the case in my data set)?
Indeed, there are. R treats NA values differently in that they can neither pass nor fail your conditions. You can change these to 0 before you subset, or (assuming the vectors take only the values 0, 1 and NA) do something like !( !Frankfurt %in% c(0,NA) & !London %in% c(0,NA)) . Not the most elegant solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.