[R] Sorting a data set

jim holtman jholtman at gmail.com
Thu May 31 05:07:03 CEST 2012


If you want to sort by the columns with the names 'var1' and 'var2',
you would do:

newdata <- data[order(data$var1, data$var2), ]



On Wed, May 30, 2012 at 6:07 PM, tony.anderson <tony.anderson at noaa.gov> wrote:
> I am a novice user of R and am stumbling on how to order a dataset produced
> during my session.
>
> I have a 1863 row X 14 column dataset that I want to put out to a file.  I
> want the output sorted by the first column and then by the second column
> both in ascending order.  The first column is character and the second is
> numeric (I hope).  I used an "as.numeric" function to assign that variable.
>  Is there a reason R would not accept "0" or "00" as a numeric value?
>
> I have tried using the order function but the examples I have seen don't
> seem to translate for me.  I tried something like this assuming my dataset
> is called "data".
>
> datanew<-data[order(var1, var2),]
> print(datanew)
>
> This generates an "incorrect number of dimensions"  error in the order
> function.  I also tried listing all the variables in the parentheses.
>
> Your help is appreciated.
>
> ______________________________________________
> 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
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list