如何找到或匹配一个数据帧作为子集(完整)到R中的另一个数据帧? [英] how find or match one data frame as a subset(full) into another data frame in R?

查看:82
本文介绍了如何找到或匹配一个数据帧作为子集(完整)到R中的另一个数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面给出的两个数据帧df1和df2. df1

I have two data frames df1 and df2 given below. df1 is

  c1       c2   c3  c4
   B  2.34000  1.00  I
   A 14.43000  2.10  J
   D  3.45515  1.00  K
   B  2.50000  2.09   
   A  2.44000  1.10  K
   K  5.00000  1.09  L

df2是:

  c1    c2   c3
   B  2.34  1.00
   A 14.43  2.10
   D  3.43  1.00
   B  2.50  2.09
   E  5.00  1.09
   A  2.44  1.10

这里的需求是这样的:这两个数据帧之间存在匹配(或比较).如果在df1中完全找到df2(表示df2的内容与df1的任何子集相匹配,而与顺序无关),则与df2df2的子集完全匹配df1df2匹配),则输出为true.如果不匹配,则返回false.

the requirement here is like this: there is matching(or comparison) between these two data frames. if df2 completely found (that means the content of df2 matched with any subset of df1 irrespective of the order) in df1(either exactly matched with df2 or subset of df1 matched with df2) then output is true. If not matched then return false.

我尝试了以下方法:

1. left_join(df2,df1)
2. merge(df2,df1)
3. inner_join(df2,df1)
4. dd1[dd1$c1 %in% dd$c1,]

以上所有方法给出的数据在两者之间是通用的,但未根据要求给出结果.

all the above methods give that data which is common in between both but not give results as per requirements.

请为我建议一些解决方案.

Please suggest me some solution for the same.

推荐答案

您可以像下面这样使用matchinteraction:

You can use match and interaction like:

df1 <- read.table(text="c1       c2   c3  c4
   B  2.34000  1.00  I
   A 14.43000  2.10  J
   D  3.45515  1.00  K
   B  2.50000  2.09  NA
   A  2.44000  1.10  K
   K  5.00000  1.09  L", header=T)

df2 <- read.table(text="c1    c2   c3
   B  2.34  1.00
   A 14.43  2.10
   D  3.43  1.00
   B  2.50  2.09
   E  5.00  1.09
   A  2.44  1.10", header=T)

!any(is.na(match(interaction(df2), interaction(df1[names(df2)]))))
#[1] FALSE

#And packed in a function
"%completelyFoundIn%" <- function(x, y) {!any(is.na(match(interaction(x), interaction(y[names(x)]))))}

df2 %completelyFoundIn% df1
#[1] FALSE

df2[c(1,2,4,6),] %completelyFoundIn% df1
#[1] TRUE

df2[-5,c(1,3)] %completelyFoundIn% df1
#[1] TRUE

这篇关于如何找到或匹配一个数据帧作为子集(完整)到R中的另一个数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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