• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    同等学历教学

    同等学历教学

    免费
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      同等学历教学

      同等学历教学

      免费
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      老俊俊的生信笔记

      • 首页
      • 博客
      • 老俊俊的生信笔记
      • R Tips :match 函数

      R Tips :match 函数

      • 发布者 weinfoadmin
      • 分类 老俊俊的生信笔记
      • 日期 2021年9月10日
      • 评论 0评论

      感谢老俊俊的大力支持。我们会每日跟新,欢迎您关注老俊俊的生信笔记。

      R Tips :match 函数

      Introduction

      在 R 数据处理和筛选中,有时候需要匹配筛选目标向量或表格,如果是乱序的就更加麻烦一些,R 里的 match 函数为我们提供了便捷的工具,先来看一看用法:

      # 查看帮助信息
      ?match()

      英文帮助信息:

      Value Matching
      Description
      match returns a vector of the positions of (first) matches of its first argument in its second.

      %in% is a more intuitive interface as a binary operator, which returns a logical vector indicating if there is a match or not for its left operand.

      Usage
      match(x, table, nomatch = NA_integer_, incomparables = NULL)

      x %in% table
      Arguments
      x
      vector or NULL: the values to be matched. Long vectors are supported.

      table
      vector or NULL: the values to be matched against. Long vectors are not supported.

      nomatch
      the value to be returned in the case when no match is found. Note that it is coerced to integer.

      incomparables
      a vector of values that cannot be matched. Any value in x matching a value in this vector is assigned the nomatch value. For historical reasons, FALSE is equivalent to NULL.

      可以看到大概的描述:

      • 1、match 函数返回第 1 个向量在第 2 个向量中的位置(下标)
      • 2、%in% 是一个更直观的二进制操作符,返回一个逻辑变量,此外该操作符就是 match 写成的一个函数:
      # %in% is currently defined as:
      "%in%" <- function(x, table) {
        match(x, table, nomatch = 0) > 0
      }

      用法:

      # Usage
      match(x, table, nomatch = NA_integer_, incomparables = NULL)
      x %in% table

      参数说明:

      • 1、x : 需要匹配的向量,支持长向量
      • 2、table : 被匹配的向量,不支持长向量
      • 3、nomatch : 没有被匹配的返回值,必须为整数型
      • 4、incomparables : 指定不能被匹配的值或向量

      测试

      先测试两个都是向量:

      # 构造两个向量
      test1 <- c('A','B','D','F','X','Y')
      test2 <- c('A','B','C','D','E','F','G')

      # 查看test1在test2中的位置
      match(test1,test2)
      [1]  1  2  4  6 NA NA
      # 指定未匹配上的返回值为0
      match(test1,test2,nomatch = 0)
      [1] 1 2 4 6 0 0
      # 取出匹配上的test1在test2中的值
      test2[match(test1,test2)]
      [1] "A" "B" "D" "F" NA  NA
      # 去除NA,取出匹配上的值
      test2[na.omit(match(test1,test2))]
      [1] "A" "B" "D" "F"

      # %in%使用
      test1 %in% test2
      [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE
      # 查看test1能够被匹配上的元素个数
      sum(test1 %in% test2)
      [1] 4
      # 取出匹配上的test1在test1中的值
      test1[test1 %in% test2]
      [1] "A" "B" "D" "F"
      # 取出没有匹配上的test1的值
      test1[!(test1 %in% test2)]
      [1] "X" "Y"
      # 取出没有匹配上的test2的值
      test2[!(test2 %in% test1)]
      [1] "C" "E" "G"

      测试向量匹配数据框:

      # 构造数据框
      dat <- data.frame(name = paste('gene',1:10,sep = '_'),exp = sample(100,10))
      dat
            name exp
      1   gene_1  14
      2   gene_2  61
      3   gene_3  40
      4   gene_4   5
      5   gene_5  91
      6   gene_6  24
      7   gene_7  65
      8   gene_8  98
      9   gene_9  92
      10  gene_10  78
      # 匹配向量
      my_gene <- c('gene_3','gene_5','gene_10','gene_12')

      # 匹配my_gene在数据框的表达值,行名是下标值
      dat[match(my_gene,dat$name),]
            name exp
      3   gene_3  40
      5   gene_5  91
      10 gene_10  78
      NA    <NA>  NA
      # 去除没有匹配到的NA值
      dat[na.omit(match(my_gene,dat$name)),]
            name exp
      3   gene_3  40
      5   gene_5  91
      10 gene_10  78
      # 用%in%操作符匹配,注意顺序得反过来,用dat$name匹配my_gene
      dat[dat$name %in% my_gene,]
            name exp
      3   gene_3  40
      5   gene_5  91
      10 gene_10  78
      # 添加which函数也能达到同样效果
      dat[which(dat$name %in% my_gene),]
            name exp
      3   gene_3  40
      5   gene_5  91
      10 gene_10  78
      # 取出未匹配上的基因表达值,注意要在括号外加!
      dat[!(dat$name %in% my_gene),]
          name exp
      1 gene_1  14
      2 gene_2  61
      4 gene_4   5
      6 gene_6  24
      7 gene_7  65
      8 gene_8  98
      9 gene_9  92

      综合下来,我感觉操作符 %in% 相对于 match 函数更加简便和人性化一些,返回值不会存在一些 NA,对于数据的匹配和筛选更加快捷和准确。

      欢迎小伙伴留言评论!

      点击我留言!

      今天的分享就到这里了,敬请期待下一篇!

      最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!

      如果觉得对您帮助很大,打赏一下吧!


      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      什么? R 版 Hisat2
      2021年9月10日

      下一篇文章

      conda 安装软件报错
      2021年9月10日

      你可能也喜欢

      8-1651542331
      跟着Nature学绘图(2) 箱线图-累积分布曲线图
      2 5月, 2022
      9-1651542322
      Julia 笔记之字符串
      2 5月, 2022
      0-1651542343
      Julia 笔记之数学运算和初等函数
      1 5月, 2022

      留言 取消回复

      要发表评论,您必须先登录。

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?