Filter rows which has at least one of particular values

时光总嘲笑我的痴心妄想 提交于 2021-01-27 16:41:37

问题


I have a data frame like this.

df
    Tour    Order   Machine    Company
[1]    A        D         D          B
[2]    B        B         A          G
[3]    A        E         B          A
[4]    C        B         C          B
[5]    A        G         G          C

I want to get the rows where the three columns Tour, Order Machine contains at least one D E or G.

The result should be:

    Tour    Order   Machine    Company
[1]    A        D         D          B
[3]    A        E         B          A
[5]    A        G         G          C

My attempt:

df %>%
    filter(any(c(Tour, Order, Machine) %in% c('D', 'E', 'G')))

But it doesn't filter correctly(all the rows are returned). Could anybody please help me?


回答1:


Another option:

df[rowSums(sapply(df[-4], '%in%', c('D', 'E', 'G'))) > 0,]

The resut:

  Tour Order Machine Company
1    A     D       D       B
3    A     E       B       A
5    A     G       G       C

With dplyr you should add rowwise():

df %>%
  rowwise() %>% 
  filter(any(c(Tour, Order, Machine) %in% c('D', 'E', 'G')))



回答2:


Another tidyverse approach using filter_at

df %>% filter_at(vars(-Company), any_vars(. %in% c("D", "E", "G")))
#  Tour Order Machine Company
#1    A     D       D       B
#2    A     E       B       A
#3    A     G       G       C



回答3:


ind <- apply(sapply(df1[c("Tour","Order","Machine")],`%in%`,c('D', 'E', 'G')),1,any)
df1[ind,]
#   Tour Order Machine Company
# 1    A     D       D       B
# 3    A     E       B       A
# 5    A     G       G       C
  • sapply will return a matrix of Booleans containing the match for each cell.
  • apply will check if any of them is TRUE, which means you want to keep the row
  • We filter input

A dplyr version:

df1 %>%
  filter_at(c("Tour","Order","Machine"),any_vars(.%in% c('D', 'E', 'G')))
#   Tour Order Machine Company
# 1    A     D       D       B
# 2    A     E       B       A
# 3    A     G       G       C

data

df1 <- read.table(header=TRUE,stringsAsFactors=FALSE,text="
 Tour    Order   Machine    Company
    A        D         D          B
    B        B         A          G
    A        E         B          A
    C        B         C          B
    A        G         G          C")



回答4:


You can lapply over the columns to check for matches, then Reduce using | (or) to select if there are any matches.

df[Reduce('|', lapply(df[-4], '%in%', c('D', 'E', 'G'))),]



回答5:


using base R:

df1[grepl("[DEG]",do.call(paste,df1[-4])),]# YOU CAN USE "D|E|G"

  Tour Order Machine Company
1    A     D       D       B
3    A     E       B       A
5    A     G       G       C


来源:https://stackoverflow.com/questions/51101022/filter-rows-which-has-at-least-one-of-particular-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!