data(mtcars)
#sort by the number of cylenders
sort(mtcars, by= ~cyl)
sort(mtcars, by= cyl) #identical: no need for ~
#sort in descending order
sort(mtcars, by= -cyl)
#break ties with horse power
sort(mtcars,by= cyl +hp )
sort(mtcars,by= cyl -hp )
#randomly permute the data
sort(mtcars,by= rnorm(nrow(mtcars)) )
#reverse order
sort(mtcars,by= nrow(mtcars):1 )
#sort by squared deviation from mean hp
sort(mtcars,by= -(hp-mean(hp))^2 )
sort(mtcars,by= "-(hp-mean(hp))^2" ) #identical
Run the code above in your browser using DataLab