LoginSignup
1
2

More than 5 years have passed since last update.

Rでベクトルに独自のソート順を与えたい時

Last updated at Posted at 2016-07-20

普通にソートしたら意図した順にならない時は

> sort(c("first", "3rd", "_second"))
[1] "_second" "3rd"     "first"  

ordered = TRUEオプションを指定して順序つきの因子にするとできる

> vec <- factor(c("first", "3rd", "_second"), levels = c("first", "_second", "3rd"), ordered = TRUE)
> vec
[1] first   3rd     _second
Levels: first < _second < 3rd
> sort(vec)
[1] first   _second 3rd    
Levels: first < _second < 3rd

Enjoy!

参考: 因子Tips大全 - RjpWiki

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2