R语言常用函数:交集intersect、并集union、找不同setdiff、判断相同setequal

在R语言进行数据分析时,经常需要找不同组间的相同和不同,那你应该掌握如下几个函数,让你事半功倍。

交集intersect

两个向量的交集,集合可以是数字、字符串等

# 两个数值向量取交集
intersect(x=1:4, y = 2:6)
# [1] 2 3 4

# 两个字符向量取交集
intersect(x=letters[1:4], y = letters[2:6])
# [1] "b" "c" "d"

# 混合向量
intersect(x=c("a", "b", "c", 4), y = c("a", 2, 3, 4))
[1] "a" "4"

并集union

求两个向量的并集,集合可以是任何数值类型

# 两个数值向量取并集
union(x=1:4, y = 2:6)
# [1] 1 2 3 4 5 6

# 两个字符向量取并集
union(x=letters[1:4], y = letters[2:6])
# [1] "a" "b" "c" "d" "e" "f"

# 混合向量
union(x=c("a", "b", "c", 4), y = c("a", 2, 3, 4))
[1] "a" "b" "c" "4" "2" "3"

找不同setdiff

求向量x与向量y中不同的元素(只取x中不同的元素)
setdiff(x, y)

x = 1:4
y = 2:6
# 找x中不同于y的元素
setdiff(x, y)
# [1] 1
# 找y中不同于x的元素
setdiff(y, x)
# [1] 5 6

判断相同setequal

x = 1:4
y = 2:6
# 判断x与y是否相同,结果为假
setequal(x, y)
# [1] FALSE
# 找y与x是否相同,结果为假
setequal(y, x)
# [1] FALSE

# 只有完全相同的才返回TRUE
y = 1:4
setequal(x, y)
# [1] TRUE

猜你喜欢

写在后面

为鼓励读者交流、快速解决科研困难,我们建立了“宏基因组”专业讨论群,目前己有国内外1500+ 一线科研人员加入。参与讨论,获得专业解答,欢迎分享此文至朋友圈,并扫码加主编好友带你入群,务必备注“姓名-单位-研究方向-职称/年级”。技术问题寻求帮助,首先阅读《如何优雅的提问》学习解决问题思路,仍末解决群内讨论,问题不私聊,帮助同行。
image

学习扩增子、宏基因组科研思路和分析实战,关注“宏基因组”
image

点击阅读原文,跳转最新文章目录阅读
https://mp.weixin.qq.com/s/5jQspEvH5_4Xmart22gjMA

  • 23
    点赞
  • 115
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
```c #include <stdio.h> #include <stdlib.h> int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int *intersect(int *nums1, int nums1Size, int *nums2, int nums2Size, int *returnSize) { int i, j, k; int *res = (int *)malloc(sizeof(int) * (nums1Size < nums2Size ? nums1Size : nums2Size)); qsort(nums1, nums1Size, sizeof(int), cmp); qsort(nums2, nums2Size, sizeof(int), cmp); i = j = k = 0; while (i < nums1Size && j < nums2Size) { if (nums1[i] < nums2[j]) { i++; } else if (nums1[i] > nums2[j]) { j++; } else { res[k++] = nums1[i]; i++; j++; } } *returnSize = k; return res; } int *unionSet(int *nums1, int nums1Size, int *nums2, int nums2Size, int *returnSize) { int i, j, k; int *res = (int *)malloc(sizeof(int) * (nums1Size + nums2Size)); qsort(nums1, nums1Size, sizeof(int), cmp); qsort(nums2, nums2Size, sizeof(int), cmp); i = j = k = 0; while (i < nums1Size && j < nums2Size) { if (nums1[i] < nums2[j]) { res[k++] = nums1[i++]; } else if (nums1[i] > nums2[j]) { res[k++] = nums2[j++]; } else { res[k++] = nums1[i]; i++; j++; } } while (i < nums1Size) { res[k++] = nums1[i++]; } while (j < nums2Size) { res[k++] = nums2[j++]; } *returnSize = k; return res; } int main() { int nums1[] = {1, 2, 3, 4, 5}; int nums2[] = {3, 4, 5, 6, 7}; int size1 = sizeof(nums1) / sizeof(int); int size2 = sizeof(nums2) / sizeof(int); int size; int i; int *res1 = intersect(nums1, size1, nums2, size2, &size); printf("交集:"); for (i = 0; i < size; i++) { printf("%d ", res1[i]); } printf("\n"); free(res1); int *res2 = unionSet(nums1, size1, nums2, size2, &size); printf("并集:"); for (i = 0; i < size; i++) { printf("%d ", res2[i]); } printf("\n"); free(res2); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值