首页 文章

从另一个向量中对R中的数据进行子集化(排除)[重复]

提问于
浏览
0

这个问题在这里已有答案:

我有一个数据框,其中包含以下元素,我希望有一个记录子集 .

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')

我想对数据帧进行子集化,使得最终数据帧包含除x中未列出的位置之外的所有元素 . 我想创建一个测试数据框,我试过这个

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

它让我反其道而行之 . 有人可以帮忙吗?

我期待这样的输出:

location year 
       london    1990
       london    1991
       london    1992
       paris     2003

3 回答

  • 0

    正如@ycw在评论中指出的那样,否定逻辑条件会给你预期的结果

    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
    

    结果

    location year
    1   london 1990
    2   london 1991
    3   london 1992
    6    paris 2003
    
  • 0

    使用Dplyr:

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

    如果您只想从原始数据框中排除几个元素,您还可以按如下方式创建子集:

    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
    

    请注意,如果您计划过滤多个元素,则效率不高 . 在那些情况下,ycw和Damian的方法更好 .

    但是,如果您只有一个或几个元素要删除,上述安排是一种简单,快速,合理的方法来实现您的目标:

    location year
    1   london 1990
    2   london 1991
    3   london 1992
    7    delhi 2001
    

相关问题