R:计算多个数据帧之间的匹配次数 [英] R: Counting the number of matches between multiple data frames

查看:22
本文介绍了R:计算多个数据帧之间的匹配次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据多个 data.frames 中唯一匹配项的 ID 找到匹配项数

I want to find the number of matches based on ID of unique matches within multiple data.frames

数据如下所示:

df1: KeyID
       x
       x
       y
       y
       z

df2: KeyID
       x
       x
       x
       z
       z

df3: KeyID
       x
       y
       y
       z

我想计算数据帧之间唯一匹配的数量.

I want to count the number of unique matches between data frames.

输出如下所示:2

因为 x 和 z 是两个集合之间唯一的匹配项.

Since x and z are the only matches between the two sets.

我已经这样做了,但想知道是否有更快的方法:

I have done this but want to know if there is a faster way:

df1.2 <- df2[df2$KeyID %in% df1$KeyID,]
length(unique(df1.2$KeyID))

有什么想法吗?

推荐答案

你可以用 intersect 来设置交集:

You can do set intersection with intersect:

v1 <- c("x", "x", "y", "y", "z")
v2 <- c("x", "x", "x", "z", "z")
intersect(v1, v2)
# [1] "x" "z"
length(intersect(v1, v2))
# [1] 2

根据 akrun 的建议,适应问题编辑,如果有多个向量,

Adapting for the question edit, as per akrun's suggestion, if there are multiple vectors,

v1 <- c("x", "x", "y", "y", "z")
v2 <- c("x", "x", "x", "z", "z")
v3 <- c("x", "y", "y", "z")
vector.list <- list(v1, v2, v3)

Reduce("intersect", vector.list)
# [1] "x" "z"

这篇关于R:计算多个数据帧之间的匹配次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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