Selecting rows from a data frame from combinations of lists [duplicate]

北战南征 提交于 2019-12-02 09:30:51

We could use dplyr::anti_join() to do the row exclusion filtering for us, if we had two dataframes:

index <- data.frame(col1 = as.character(filter[,1]),
                    col2 = filter[,2])

anti_join(dat, index)

Joining, by = c("col1", "col2")
  col1 col2 col3
1    4    x    d
2    1    y    e
3    2    z    f
4    3    x    g
5    4    y    h
6    1    z    i
7    2    x    j
8    3    y    k
9    4    z    l

mostly base with a little help from dplyr:

dplyr::setdiff(dat,merge(dat,setNames(as.data.frame(filter),names(dat)[1:2])))

  col1 col2 col3
1    4    x    d
2    1    y    e
3    2    z    f
4    3    x    g
5    4    y    h
6    1    z    i
7    2    x    j
8    3    y    k
9    4    z    l

A real base R solution though not so pretty and you lose the row order:

subset(merge(dat,`[[<-`(setNames(as.data.frame(filter),names(dat)[1:2]),"x",value=1),all.x=T),is.na(x),-4)

   col1 col2 col3
2     1    y    e
3     1    z    i
4     2    x    j
6     2    z    f
7     3    x    g
8     3    y    k
10    4    x    d
11    4    y    h
12    4    z    l
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!