简体   繁体   English

根据R中的目标向量对数据帧组进行排序

[英]Sort dataframe group according to a target vector in R

I have a dataframe such as shown below (original data is really big). 我有一个如下所示的数据帧(原始数据非常大)。 I want to sort them based on the group of target vector and get the result. 我想根据目标矢量组对它们进行排序并获得结果。

dataframe 数据帧

sample     group    
A           AA 
B           CC
C           CC
D           BB
E           AA
F           AA

target.vector <- c("AA", "BB", "CC")

result 结果

 sample     group    
    A           AA 
    E           AA
    F           AA
    D           BB
    B           CC
    C           CC

You could turn group into a factor and then use order eg for your sample data 您可以将group转换为factor ,然后使用order例如您的样本数据

...
df$group <- factor(df$group, levels=c('AA', 'CC', 'BB'))
df[order(df$group), ]

would result in 会导致

  sample group
1      A    AA
5      E    AA
6      F    AA
2      B    CC
3      C    CC
4      D    BB

We can use data.table . 我们可以使用data.table Convert the 'data.frame' to 'data.table', set the key as 'group' and join with the 'target.vector' 将'data.frame'转换为'data.table',将key设置为'group'并与'target.vector'连接

library(data.table)
setDT(df1, key = "group")[target.vector]
#     sample group
#1:      A    AA
#2:      E    AA
#3:      F    AA
#4:      D    BB
#5:      B    CC
#6:      C    CC

Changing the 'target.vector' does change the order 更改'target.vector'会改变顺序

target.vector <- c("BB", "AA", "CC")
setDT(df1, key = "group")[target.vector]
#   sample group
#1:      D    BB
#2:      A    AA
#3:      E    AA
#4:      F    AA
#5:      B    CC
#6:      C    CC

Or a base R option would be match 或者base R选项match

df1[with(df1, order(match(group, target.vector))),]
#   sample group
#1      A    AA
#2      E    AA
#3      F    AA
#4      D    BB
#5      B    CC
#6      C    CC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM