R:找到两个列表的共同元素数 [英] R: find the number of common elements of two lists

查看:85
本文介绍了R:找到两个列表的共同元素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这两个列表,我想弄清楚这两个列表中的列元素的数量:

Let's say I have these two lists, and I want to figure out the number of column elements among these two lists:

> xl
[[1]]
[1] 1 2 3 4 5

> yl
[[1]]
[1] 4 3 5 6 7

因此,在此示例中,答案将是3.有什么建议吗?

So, in this example, the answer would be 3. Any suggestions?

推荐答案

使用intersect:

x1 = list(c(1, 2, 3, 4, 5))
y1 = list(c(4, 3, 5, 6, 7))

length(intersect(x1[[1]], y1[[1]]))
# 3

这篇关于R:找到两个列表的共同元素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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