0

I have a data frame which has the following elements and I want to have a subset of records.

location <- c('london', 'london','london', 'newyork' ,'newyork', 'paris', 'delhi')
year<- c(1990, 1991, 1992, 2001, 2002, 2003,2001)

df<- data.frame(location,year)

and I have a vector say

x<- c('newyork', 'delhi')

I want to subset the data frame such that the final data frame contains all elements except the location that are not listed in x. I would like to create a test data frame, I have tried this

 test1 <- df[df$location %in% c('newyork','delhi'), ] 

It gives me the opposite. Can some one help?

I am expecting the output like this:

       location year 
       london    1990
       london    1991
       london    1992
       paris     2003
www
  • 38,575
  • 12
  • 48
  • 84
user3570187
  • 1,743
  • 3
  • 17
  • 34

3 Answers3

0

Using Dplyr:

new_df <- df %>% 
  filter(!(location %in% c("newyork", "delhi")))
AaronT86
  • 53
  • 4
0

as @ycw pointed out in the comment, the negating the logical condition will give you the expected result

location <- c('london', 'london','london', 'newyork' ,'newyork', 'paris', 'delhi')
year <- c(1990, 1991, 1992, 2001, 2002, 2003,2001)

df <- data.frame(location, year)

x <- c('newyork', 'delhi')

# add"!" to the subset condition
test1 <- df[ !df$location %in% c('newyork','delhi'), ] 

test1

Result

  location year
1   london 1990
2   london 1991
3   london 1992
6    paris 2003
Damian
  • 1,385
  • 10
  • 10
0

If there's only a couple elements you want to exclude from the original data frame, you could also create the subset as follows:

location <- c('london', 'london','london', 'newyork' ,'newyork', 
'paris', 'delhi')
year<- c(1990, 1991, 1992, 2001, 2002, 2003,2001)

df<- data.frame(location,year)

# Identify which elements you wish to remove and precede with NOT operator (!)
df2 <- df[!df$location=="newyork" & !df$location=="paris",]

df2

Take note this isn't very efficient if you plan to filter multiple elements. In those cases, ycw and Damian's approach is better.

However, if you only have a single or couple elements to remove, the above arrangement is an easy, quick, logical method to achieve what you're after:

 location year
1   london 1990
2   london 1991
3   london 1992
7    delhi 2001
RVD
  • 100
  • 1
  • 9