[R] Trying to understand how to sort a DF on two columns

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Aug 13 10:11:15 CEST 2019


Though good answers were already given, I would like to say something.

If you are lazy (typing), use with, if you prefer to play safe, don't.
I am lazy many times, but in interactive mode only.

I find it better in the long run *not* to take advantage of R's 
one-liners, they tend to be less readable. Instead of putting everything 
in the same instruction why not

i <- order( temp$patid, temp$time )
data4xsort <- temp[ i, ]

This has the disadvantage of creating an extra variable but are you 
really having memory problems? If not, use the clearer code. Besides, if 
this goes into a function all temporary variables will be gone and the 
memory released, in which case there will be no problem.

(The with equivalent is i <- with(temp, order(patid, time)), btw.)

Hope this helps,

Rui Barradas

Às 03:20 de 13/08/19, Sorkin, John escreveu:
> I want to sort a DF, temp, on two columns, patid and time. I have searched the internet and found code that I was able to modify to get my data sorted. Unfortunately I don't understand how the code works. I would appreciate it if someone could explain to me how the code works. Among other questions, despite reading, I don't understand how with() works, nor what it does in the current setting.
> code:
> data4xsort<-temp[
>    with( temp, order(temp[,"patid"], temp[,"time"])),
> ]
> Thank you,
> John
> John David Sorkin M.D., Ph.D.
> Professor of Medicine
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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