当前位置:网站首页>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~~~
边栏推荐
猜你喜欢

golang modules initialization project

Smart Contract Security - Private Data Access

阿里CTO程立:阿里巴巴的开源历程、理念和实践

Metaverse Post Office AI space-themed series of digital collections will be launched at 10:00 on July 30th "Yuanyou Digital Collection"

5G-based Warehousing Informatization Solution 2022

In-depth analysis of Kubernetes application management

基于5G的仓储信息化解决方案2022

ECCV 2022 | Towards Data Efficient Transformer Object Detectors

Installing and Uninstalling MySQL on Mac
4位资深专家多年大厂经验分享出Flink技术内幕架构设计与实现原理
随机推荐
ESP32 Repeated Reboot Issue Arduino Shield Power Outage Detector
71-page comprehensive overall solution for global tourism 2021 ppt
Redis cache penetration, breakdown, avalanche and consistency issues
面试何惧调优!腾讯技术官私藏的性能优化方案手册,原理实战齐全
【云原生】灰度发布、蓝绿发布、滚动发布、灰度发布解释
GeoServer
Huawei issues another summoning order for "Genius Boys"!He, who had given up an annual salary of 3.6 million, also made his debut
SQL 优化这么做就对了!
The use and principle of distributed current limiting reduction RRateLimiter
Android jump to google app market
2022最新 | 室外单目深度估计研究综述
我们公司用了 6 年的网关服务,动态路由、鉴权、限流等都有,稳的一批!
Go to Tencent for an interview and let people turn left directly: I don't know idempotency!
基于5G的仓储信息化解决方案2022
关于MySQL主从复制的数据同步延迟问题
Ts是什么?
JUC常见的线程池源码学习 02 ( ThreadPoolExecutor 线程池 )
Use of SLF4J
Redis 缓存穿透、击穿、雪崩以及一致性问题
Our company has used gateway services for 6 years, dynamic routing, authentication, current limiting, etc., a stable batch!