删除r中另一个data.frame中data.frame的确切行和行频 [英] Remove exact rows and frequency of rows of a data.frame that are in another data.frame in r

查看:92
本文介绍了删除r中另一个data.frame中data.frame的确切行和行频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下两个data.frames:

Consider the following two data.frames:

a1 <- data.frame(A = c(1:5, 2, 4, 2), B = letters[c(1:5, 2, 4, 2)])
a2 <- data.frame(A = c(1:3,2), B = letters[c(1:3,2)])

我想删除a2a1的确切行,以便结果应为:

I would like to remove the exact rows of a1 that are in a2 so that the result should be:

A  B
4  d
5  e
4  d
2  b

请注意,最终结果中保留a1中带有2 b的一行.当前,我使用循环语句,因为我的data.frames中有很多变量和成千上万的行,所以它变得非常慢.有任何内置函数可以得到此结果吗?

Note that one row with 2 b in a1 is retained in the final result. Currently, I use a looping statement, which becomes extremely slow as I have many variables and thousands of rows in my data.frames. Is there any built-in function to get this result?

推荐答案

我想这类似于 DWal的解决方案,但在基数R

I guess this is similar to DWal's solution but in base R

a1_temp = Reduce(paste, a1)
a1_temp = paste(a1_temp, ave(seq_along(a1_temp), a1_temp, FUN = seq_along))

a2_temp = Reduce(paste, a2)
a2_temp = paste(a2_temp, ave(seq_along(a2_temp), a2_temp, FUN = seq_along))

a1[!a1_temp %in% a2_temp,]
#  A B
#4 4 d
#5 5 e
#7 4 d
#8 2 b

这篇关于删除r中另一个data.frame中data.frame的确切行和行频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆