当前位置:网站首页>R中按照数字大小进行排序
R中按照数字大小进行排序
2022-07-30 14:44:00 【Listenlii-生物信息知识分享】
在R中有时会需要通过数字大小对某些数据进行排序。
不过R默认是按照字符大小顺序进行排序,如常见的OTU名称:
OTU1,OTU2,OTU3,OTU10 ,OTU20...
会被默认排序为:
OTU1,OTU10,OTU2,OTU20,OTU3...
这在一些数据处理和画图过程非常不方便。
如果要按照数字排序为OTU1,OTU2,OTU10这种,可以有很多方法,本文举几种简单的例子:
先读进一个OTU表~
otu = read.table(file = "otu.txt",sep="\t",row.names = 1,header=T,check.names=F)
# 行名为OTU的名字
name = rownames(OTU)
1. gtools包的mixedorder函数,一步到位直接对OTU名字中的数字排序:
library(gtools)
a = mixedorder(name)
otu2 = otu[a,]
2. stringr包的str_order函数类似:
library(stringr)
b = str_order(name, numeric = TRUE)
otu2 = otu[b,]
3. OTU名字去掉OTU只保留数字再排序:
c = order(as.numeric(gsub("OTU","",name)))
otu2 = otu[c,]
4.OTU名字中的OTU和数字分开,单独对数字排序:
d = order(as.numeric(sapply(strsplit(name,"Zotu"),"[",2)))
otu2 = otu[d,]
5.根据OTU名字的字节数进行排序:
e = name[order(nchar(name),name)]
otu2 = otu[e,]
还有其他多种方法,不再赘述,Over~~~
边栏推荐
- Delayed message queue
- MySql报错:SqlError(Unable to execute query“, “Can‘t create/write to file OS errno 2 - No such file...
- This editor actually claims to be as fast as lightning!
- Distributed pre-course: MySQL implements distributed locks
- 存储器映射、位带操作
- Flink real-time data warehouse completed
- MaxWell scraped data
- 算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
- golang图片处理库image简介
- 瑞吉外卖项目实战Day02
猜你喜欢
随机推荐
(科普文)什么是碎片化NFT(Fractional NFT)
English语法_不定代词 - both / either / neither
Get the Google Advertising ID as a unique identifier
HUAWEI CLOUD Releases Open Source Software Governance Service - Software Component Analysis
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
Ts是什么?
Meta首份元宇宙白皮书9大看点,瞄准80万亿美元市场
timed task corn
Flink实时仓库-DWS层(关键词搜索分析-自定义函数,窗口操作,FlinkSql设置水位线,保存数据到Clickhouse)模板代码
Smart Contract Security - Private Data Access
localhost with 127.0.0.1
阿里CTO程立:阿里巴巴的开源历程、理念和实践
GeoServer
Lock wait timeout exceeded solution
延时消息队列
90后人大硕士为学医竟重新高考,成功被首医大录取
SLF4J的使用
有关收集箱的改进建议
CVE-2022-33891 Apache Spark 命令注入复现
[深入研究4G/5G/6G专题-46]: 5G Link Adaption链路自适应-2-常见缩略语









