Delete rows in data frame based on multiple columns from another data frame in R

Maya

I would like to remove rows that have specific values for columns that match values in another data frame.

a<-c(1,1,2,2,2,4,5,5,5,5)
b<-c(10,10,22,30,30,30,40,40,40,40)
c<-c(1,2,1,2,2,2,2,1,1,2)
d<-rnorm(1:10)
data<-data.frame(a,b,c,d)

a<-c(2,5)
b<-c(30,40)
c<-c(2,1)
x<-data.frame(a,b,c)

So that y can become:

 a  b c          d
 1 10 1 -0.2509255
 1 10 2  0.4142277
 2 22 1 -0.1340514
 4 30 2 -1.5372009
 5 40 2  1.9001932
 5 40 2 -1.2825212

I tried the following, which did not work:

y<-data[!data$a==a & !data$b==b & !data$c==c,] 

y<-subset(data, !data$a==x$a & !data$b==x$b & !data$c==x$c)

I also tried to just flag the ones that should be removed in order to subset in a second step, but this did not work either:

y<-data
y$rm<-ifelse(y$a==x$a & y$b==x$b & y$c==x$c, 1, 0)

The real "data" and "x" are much longer, and there are variable number of rows in data that match each row in x.

akrun

We can use anti_join from dplyr. It will return all rows from 'data' that are not matching values in 'x'. We specify the variables to be considered in the by argument.

library(dplyr)
anti_join(data, x, by=c('a', 'b', 'c'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deleting rows and columns of a data frame based on values of another data frame

From Dev

In Pandas, how to delete rows from a Data Frame based on another Data Frame?

From Dev

delete columns in data frame not in common with another (R)

From Dev

R: Filter rows based on another data frame

From Dev

R: Filter rows based on another data frame

From Dev

Extract rows from data frame based on multiple identifiers in another data frame

From Dev

Extract rows from data frame based on multiple identifiers in another data frame

From Dev

delete elements from a data frame w.r.t columns of another data frame

From Dev

R: Removing multiple columns from data frame based on vector values

From Dev

Remove rows of a data frame, based on the connection between multiple columns

From Dev

Remove rows of a data frame, based on the connection between multiple columns

From Dev

For with if loop in R, running on the data frame with multiple columns and multiple rows

From Dev

Rename records in multiple columns based on another data frame

From Dev

creating and populating multiple columns with a loop based on the values in another data frame

From Dev

Aggregate multiple rows of the same data.frame in R based on common values in given columns

From Dev

Scala code to label rows of data frame based on another data frame

From Java

Change values for entire rows based on another data frame R

From Dev

In R, how do I delete rows in a data frame by column names of another data frame?

From Dev

R aggregate rows of a data frame over multiple columns with different operators

From Dev

Sum rows in data frame based on two columns

From Dev

populate values from another data frame based on predefined set of columns

From Dev

Update few rows and columns of data.frame from another data.frame using dplyr or other solution

From Dev

Comparison of two rows based on another data frame

From Dev

Comparison of two rows based on another data frame

From Java

In R, how do I match values from selected rows in one data frame with selected columns in another?

From Dev

How can I rename all columns of a data frame based on another data frame in R?

From Dev

How to delete specific rows from a data frame on R.

From Dev

python split data frame columns into multiple rows

From Dev

Copying multiple columns from one data.frame to another

Related Related

  1. 1

    Deleting rows and columns of a data frame based on values of another data frame

  2. 2

    In Pandas, how to delete rows from a Data Frame based on another Data Frame?

  3. 3

    delete columns in data frame not in common with another (R)

  4. 4

    R: Filter rows based on another data frame

  5. 5

    R: Filter rows based on another data frame

  6. 6

    Extract rows from data frame based on multiple identifiers in another data frame

  7. 7

    Extract rows from data frame based on multiple identifiers in another data frame

  8. 8

    delete elements from a data frame w.r.t columns of another data frame

  9. 9

    R: Removing multiple columns from data frame based on vector values

  10. 10

    Remove rows of a data frame, based on the connection between multiple columns

  11. 11

    Remove rows of a data frame, based on the connection between multiple columns

  12. 12

    For with if loop in R, running on the data frame with multiple columns and multiple rows

  13. 13

    Rename records in multiple columns based on another data frame

  14. 14

    creating and populating multiple columns with a loop based on the values in another data frame

  15. 15

    Aggregate multiple rows of the same data.frame in R based on common values in given columns

  16. 16

    Scala code to label rows of data frame based on another data frame

  17. 17

    Change values for entire rows based on another data frame R

  18. 18

    In R, how do I delete rows in a data frame by column names of another data frame?

  19. 19

    R aggregate rows of a data frame over multiple columns with different operators

  20. 20

    Sum rows in data frame based on two columns

  21. 21

    populate values from another data frame based on predefined set of columns

  22. 22

    Update few rows and columns of data.frame from another data.frame using dplyr or other solution

  23. 23

    Comparison of two rows based on another data frame

  24. 24

    Comparison of two rows based on another data frame

  25. 25

    In R, how do I match values from selected rows in one data frame with selected columns in another?

  26. 26

    How can I rename all columns of a data frame based on another data frame in R?

  27. 27

    How to delete specific rows from a data frame on R.

  28. 28

    python split data frame columns into multiple rows

  29. 29

    Copying multiple columns from one data.frame to another

HotTag

Archive