我有以下数据框:
Accession Order
1 CP000517 7
2 CP002081 8
3 CP002427 9
4 CP002429 10
5 CP002430_plasmid 11
6 CP003799 12
7 CP009907 13
8 CP009908_plasmid 14
9 CP011386 15
10 CP012381 16
11 CP016827 17
12 One 3
13 One_plasmid 4
14 Two 5
15 Two_plasmid 6
16 Three 1
17 Three_plasmid 2
现在我想根据“订单”栏目订购。此外,“加入”的级别必须具有此级别顺序......
答案 0 :(得分:1)
我们只需要使用order
df2 <- df1[order(df1$Order),]
row.names(df2) <- NULL
df2$Accession <- factor(df2$Accession, levels = unique(df2$Accession))
或使用tidyverse
library(dplyr)
df1 %>%
arrange(Order) %>%
mutate(Accession = factor(Accession, levels = unique(Accession)))
或使用data.table
library(data.table)
setDT(df1)[order(Order)][, Accession := actor(Accession, levels = unique(Accession))]