[R] Sorting

jim holtman jholtman at gmail.com
Wed Sep 23 14:10:49 CEST 2009


Here is a way of doing it

> x <- read.table(textConnection("Category     Value
+ b                1
+ b                2
+ a                7
+ a                1"), header=TRUE, as.is=TRUE)
> # now keep level in original order
> x$Category <- factor(x$Category, levels=unique(x$Category))
> str(x)
'data.frame':   4 obs. of  2 variables:
 $ Category: Factor w/ 2 levels "b","a": 1 1 2 2
 $ Value   : int  1 2 7 1
> levels(x$Category)
[1] "b" "a"


On Wed, Sep 23, 2009 at 3:16 AM, Chris Li <chrisli at austwaterenv.com.au> wrote:
>
> Hello,
>
> Say I have a dataset as followed:
>
> Category     Value
> b                1
> b                2
> a                7
> a                1
>
> Then, if I:
>
> levels(Category)
>
> It will return:
> [a], [b]
>
> But I want to keep the original order, i.e.:
> [b], [a]
>
> Is it possible to do it in R?
>
> Thanks in advance!
>
> Chris
> --
> View this message in context: http://www.nabble.com/Sorting-tp25531007p25531007.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list