在 r 中只保留字符串中的唯一元素 [英] keep only unique elements in string in r

查看:35
本文介绍了在 r 中只保留字符串中的唯一元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基因组学研究中,您经常会遇到许多具有重复基因名称的字符串.我想找到一种仅将唯一基因名称保留在字符串中的有效方法.这是一个有效的例子.但是,是否可以一步完成此操作,即不必拆分整个字符串,然后将唯一元素重新组合在一起?

In genomics research, you often have many strings with duplicate gene names. I would like to find an efficient way to only keep the unique gene names in a string. This is an example that works. But, isn't it possible to do this in one step, i.e., without having to split the entire string and then having to past the unique elements back together?

genes <- c("GSTP1;GSTP1;APC")
a <- unlist(strsplit(genes, ";"))
paste(unique(a), collapse=";")
[1] "GSTP1;APC"

推荐答案

另一种做法是

unique(unlist(strsplit(genes, ";")))
#[1] "GSTP1" "APC"

那么这应该给你答案

paste(unique(unlist(strsplit(genes, ";"))), collapse = ";")
#[1] "GSTP1;APC"

这篇关于在 r 中只保留字符串中的唯一元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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