user6678274 user6678274 - 4 months ago 5
R Question

Sort column in new data

In R I have

data


Name Number
Peter 51
Phil 12
...


I want to create a new data- set where the
number
are sorted. How do I do that?

Answer

Use order function

mydata[order(Number),]

Try below

name<-c('peter','phil')
number<-c(51,12)
mydata<-data.frame(name,number)
mydata[order(number),]

Output :

   name number
2  phil     12
1 peter     51