R:从一个数据框中删除另一个数据框中的行

时间:2022-11-11 12:42:32

I have two data frames df1 and df2. They have the same (two) columns. I want to remove the rows from df1 that are in df2.

我有两个数据帧df1和df2。它们具有相同的(两列)列。我想从df2中删除df1中的行。

2 个解决方案

#1


8  

You can do that with several packages. But here's how to do it with base R.

你可以用几个包来做到这一点。但是这里是如何用基础R做的。

df1 <-matrix(1:6,ncol=2,byrow=TRUE)
df2 <-matrix(1:10,ncol=2,byrow=TRUE)
all <-rbind(df1,df2) #rbind the columns
#use !duplicated fromLast = FALSE and fromLast = TRUE to get unique rows.
all[!duplicated(all,fromLast = FALSE)&!duplicated(all,fromLast = TRUE),] 

     [,1] [,2]
[1,]    7    8
[2,]    9   10

#2


3  

Try this:

df2 <-matrix(1:6,ncol=2,byrow=TRUE)
df1 <-matrix(1:10,ncol=2,byrow=TRUE)

data.frame(v1=setdiff(df1[,1], df2[,1]), v2=setdiff(df1[,2], df2[,2]))
  v1 v2
1  7  8
2  9 10

Note that df1 and df2 are the same as Lapointe's but in the other way around, because you want to remove the rows from df1 that are in df2, so setdiff removes elements from x that are contained in y. See ?setdiff

请注意,df1和df2与Lapointe相同,但在另一方面,因为你想从df2中删除df2中的行,所以setdiff从x中删除y中包含的元素。见?setdiff

you'll get the same result as Lapointe's

你会得到与Lapointe相同的结果

#1


8  

You can do that with several packages. But here's how to do it with base R.

你可以用几个包来做到这一点。但是这里是如何用基础R做的。

df1 <-matrix(1:6,ncol=2,byrow=TRUE)
df2 <-matrix(1:10,ncol=2,byrow=TRUE)
all <-rbind(df1,df2) #rbind the columns
#use !duplicated fromLast = FALSE and fromLast = TRUE to get unique rows.
all[!duplicated(all,fromLast = FALSE)&!duplicated(all,fromLast = TRUE),] 

     [,1] [,2]
[1,]    7    8
[2,]    9   10

#2


3  

Try this:

df2 <-matrix(1:6,ncol=2,byrow=TRUE)
df1 <-matrix(1:10,ncol=2,byrow=TRUE)

data.frame(v1=setdiff(df1[,1], df2[,1]), v2=setdiff(df1[,2], df2[,2]))
  v1 v2
1  7  8
2  9 10

Note that df1 and df2 are the same as Lapointe's but in the other way around, because you want to remove the rows from df1 that are in df2, so setdiff removes elements from x that are contained in y. See ?setdiff

请注意,df1和df2与Lapointe相同,但在另一方面,因为你想从df2中删除df2中的行,所以setdiff从x中删除y中包含的元素。见?setdiff

you'll get the same result as Lapointe's

你会得到与Lapointe相同的结果