[R] Sorting a data set

arun smartpink111 at yahoo.com
Thu May 31 06:03:40 CEST 2012


Hi Tony,

Try this:
dataT<-data.frame(Var1=rep(c("Sole","Lack","ABD","Zad"),rep(5,4)), Var2=rnorm(20,0.5),Var3=runif(20,0.4))
 dataT1<-dataT[with(dataT,order(Var1,Var2,Var3)),]
dataT1
  Var1        Var2      Var3
12  ABD -0.19842353 0.4333720
13  ABD  0.14050814 0.9194297
11  ABD  1.07544531 0.4539302
14  ABD  1.17039127 0.7840392
15  ABD  1.23533897 0.5105670
6  Lack -0.14460512 0.7106342
10 Lack  0.36935316 0.6118821
9  Lack  0.62868056 0.5915753
-----------------------------------------------


A.K.





----- Original Message -----
From: tony.anderson <tony.anderson at noaa.gov>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, May 30, 2012 6:07 PM
Subject: [R] Sorting a data set

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.




More information about the R-help mailing list