R中的混合阶数据帧 [英] Mixedorder data frame in R

查看:81
本文介绍了R中的混合阶数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框:

I have a data frame which looks like this :

a = c("1A","10A","11A","2B","2C","22C","3A","3B")
b= c(1,2,3,4,5,6,7,8)
ab = data.frame(a,b)

,我想根据列a对其进行排序.我尝试了混合顺序

and I want to sort it according to the column a. I tried the mixed order

library(gtools)
ab[mixedorder(ab$a),]

但是我没有得到想要的结果(1A,2B,2C,3A,3B ..). 我该如何解决?

but I don' t get the result I want (1A,2B,2C,3A,3B..). How can I fix this?

推荐答案

我们需要转换为character类(因为'a'列是factor,基于data.frame调用中的默认选项,即)

We need to convert to character class (as the 'a' column is factor based on the default option in data.frame call i.e. stringsAsFactors=TRUE)

ab[mixedorder(as.character(ab$a)),]
#    a b
#1  1A 1
#4  2B 4
#5  2C 5
#7  3A 7
#8  3B 8
#2 10A 2
#3 11A 3
#6 22C 6

此处

这篇关于R中的混合阶数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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