Sorkin, John
2019-Aug-13 02:20 UTC
[R] Trying to understand how to sort a DF on two columns
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]]
Bert Gunter
2019-Aug-13 02:35 UTC
[R] Trying to understand how to sort a DF on two columns
https://stackoverflow.com/questions/2315601/understanding-the-order-function Do a web search on "How does order() work R" or similar for more. I can't explain with() any better than the docs: saying that it evaluates the expression argument in the data argument environment -- a data frame for the data frame method -- probably won't help you. -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Aug 12, 2019 at 7:20 PM Sorkin, John <jsorkin at som.umaryland.edu> wrote:> 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 at 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. >[[alternative HTML version deleted]]
David Winsemius
2019-Aug-13 02:43 UTC
[R] Trying to understand how to sort a DF on two columns
On 8/12/19 7:20 PM, Sorkin, John wrote:> 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, > JohnThe `order`-function returns a numeric vector which is the length of its inputs (and there is recycling when the inputs are of different length). The numbers are the order in which the items would be if they were sorted smallest to largest. There are arguments that let you control the behavior in the case of ties. So when used in an indexing application as seen here, it results in the dataframe returned with its rows in ascending order based primarily on its first argument, patid,? and in case of ties on the second argument, time. If you put a minus sign in from of the argument it the ordering is largest to smallest. If that is code you are getting from elsewhere, you should realize that it is somewhat redundant and you should question the level of R skills of its author.? In this code it is doing absolutely nothing. The `with( ...) is not needed because the arguments already have an unambiguous place to get the column names.? A more compact expression if you were going to use `with` would be: data4xsort<-temp[ with( temp, order(patid, time)), ] But using `with` carries risks because there are sometimes confusion about which environment it will find the named objects or columns. Safer would be to not using it in this situation. Your headers suggest you are using Outlook. Surely there must be a way to specify a plain text format for outgoing emails. This is a plain text mailing list. David.> > 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 at 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.
Sorkin, John
2019-Aug-13 02:44 UTC
[R] Trying to understand how to sort a DF on two columns
Bert, Thank you for your reply (and the many other questions to the list that you answer). I understand how order works when ordering based on a single column. What I don?t understand is how the code I included with my email works. I believe my problem is a lack of understanding of what with does. I have read about the with function, but I must be missing something. Thank you, John From: Bert Gunter <bgunter.4567 at gmail.com> Sent: Monday, August 12, 2019 10:36 PM To: Sorkin, John <jsorkin at som.umaryland.edu> Cc: r-help at r-project.org (r-help at r-project.org) <r-help at r-project.org> Subject: Re: [R] Trying to understand how to sort a DF on two columns https://stackoverflow.com/questions/2315601/understanding-the-order-function Do a web search on "How does order() work R" or similar for more. I can't explain with() any better than the docs: saying that it evaluates the expression argument in the data argument environment -- a data frame for the data frame method -- probably won't help you. -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Aug 12, 2019 at 7:20 PM Sorkin, John <jsorkin at som.umaryland.edu<mailto:jsorkin at som.umaryland.edu>> wrote: 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 at r-project.org<mailto:R-help at 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. [[alternative HTML version deleted]]
Rui Barradas
2019-Aug-13 08:11 UTC
[R] Trying to understand how to sort a DF on two columns
Hello, Though good answers were already given, I would like to say something. 1. 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. 2. 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 at 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. >