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

2019-08-12 Thread Jeff Newmiller
With this as an example, no wonder you don't understand it. This is horrible.

`with` is very much like the `subset` function... it alleviates the need to 
re-type the containing object name repeatedly.

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

is the same as

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

The example you gave makes no use of the `with` function.

On August 12, 2019 7:44:29 PM PDT, "Sorkin, John"  
wrote:
>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 
>Sent: Monday, August 12, 2019 10:36 PM
>To: Sorkin, John 
>Cc: r-help@r-project.org (r-help@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
>mailto:jsor...@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@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]]
>
>__
>R-help@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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@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.


Re: [R] separate and gather functions

2019-08-12 Thread reichmanj
William

 

Yes that works a little better as I don’t have to create notional variables. 
Thank you

 

Jeff

 

From: William Dunlap  
Sent: Monday, August 12, 2019 6:46 PM
To: Jeff Reichman 
Cc: r-help@r-project.org
Subject: Re: [R] separate and gather functions

 

This one uses only core R functions.  Does that count toward "elegance"?

 

> # your data, I assume, in a form one can copy and paste into R

> d <- data.frame(stringsAsFactors = FALSE,
Col1 = c("Agency A", "Agency B", "Agency C"),
Col2 = c("Function1, Function2, Function3, Function4", 
"Function2, Function4", "Function1, Function3, Function4"))

> # split Col2 by comma following by any number of spaces

> tmp <- strsplit(d$Col2, split=", *")
> data.frame(Col1 = rep(d$Col1, lengths(tmp)), Col2 = unlist(tmp), 
> stringsAsFactors=FALSE)
  Col1  Col2
1 Agency A Function1
2 Agency A Function2
3 Agency A Function3
4 Agency A Function4
5 Agency B Function2
6 Agency B Function4
7 Agency C Function1
8 Agency C Function3
9 Agency C Function4

 

Bill Dunlap
TIBCO Software
wdunlap tibco.com  

 

 

On Mon, Aug 12, 2019 at 4:23 PM mailto:reichm...@sbcglobal.net> > wrote:

R-Help Forum



I have a data set from which I have extracted two columns Column 1 is a
listing of Federal agencies and Column 2 lists functions like this



Col1   Col2

Agency A Function1, Function2, Function3, Function4

Agency B  Function2, Function4

Agency C  Function1, Function3, Function4



What I need is a long list like this



Col1   Col2

Agency A Function1

Agency A Function2

Agency 4  Function3 etc



Is there a more elegant /efficient way other than what I did which was to
separate Col2 into separate columns and then gather the data back up



myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2", "x3",
"x4", "x5"), sep = ",")  

myDat4 <- gather(data=myDat3, key="type", value = "Value",
"x1","x2","x3","x4","x5", na.rm = TRUE)



while it works, looking for a more elegant solution, if there is one



Jeff


[[alternative HTML version deleted]]

__
R-help@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]]

__
R-help@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.


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

2019-08-12 Thread Sorkin, John
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 
Sent: Monday, August 12, 2019 10:36 PM
To: Sorkin, John 
Cc: r-help@r-project.org (r-help@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 
mailto:jsor...@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@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]]

__
R-help@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.


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

2019-08-12 Thread David Winsemius

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,
John



The `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@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.


__
R-help@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.


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

2019-08-12 Thread Bert Gunter
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 
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@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]]

__
R-help@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.


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

2019-08-12 Thread Sorkin, John
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@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.


Re: [R] separate and gather functions

2019-08-12 Thread William Dunlap via R-help
This one uses only core R functions.  Does that count toward "elegance"?

> # your data, I assume, in a form one can copy and paste into R
> d <- data.frame(stringsAsFactors = FALSE,
Col1 = c("Agency A", "Agency B", "Agency C"),
Col2 = c("Function1, Function2, Function3, Function4",
"Function2, Function4", "Function1, Function3, Function4"))
> # split Col2 by comma following by any number of spaces
> tmp <- strsplit(d$Col2, split=", *")
> data.frame(Col1 = rep(d$Col1, lengths(tmp)), Col2 = unlist(tmp),
stringsAsFactors=FALSE)
  Col1  Col2
1 Agency A Function1
2 Agency A Function2
3 Agency A Function3
4 Agency A Function4
5 Agency B Function2
6 Agency B Function4
7 Agency C Function1
8 Agency C Function3
9 Agency C Function4

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Mon, Aug 12, 2019 at 4:23 PM  wrote:

> R-Help Forum
>
>
>
> I have a data set from which I have extracted two columns Column 1 is a
> listing of Federal agencies and Column 2 lists functions like this
>
>
>
> Col1   Col2
>
> Agency A Function1, Function2, Function3, Function4
>
> Agency B  Function2, Function4
>
> Agency C  Function1, Function3, Function4
>
>
>
> What I need is a long list like this
>
>
>
> Col1   Col2
>
> Agency A Function1
>
> Agency A Function2
>
> Agency 4  Function3 etc
>
>
>
> Is there a more elegant /efficient way other than what I did which was to
> separate Col2 into separate columns and then gather the data back up
>
>
>
> myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2", "x3",
> "x4", "x5"), sep = ",")
>
> myDat4 <- gather(data=myDat3, key="type", value = "Value",
> "x1","x2","x3","x4","x5", na.rm = TRUE)
>
>
>
> while it works, looking for a more elegant solution, if there is one
>
>
>
> Jeff
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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]]

__
R-help@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.


[R] separate and gather functions

2019-08-12 Thread reichmanj
R-Help Forum

 

I have a data set from which I have extracted two columns Column 1 is a
listing of Federal agencies and Column 2 lists functions like this

 

Col1   Col2

Agency A Function1, Function2, Function3, Function4

Agency B  Function2, Function4

Agency C  Function1, Function3, Function4

 

What I need is a long list like this

 

Col1   Col2

Agency A Function1

Agency A Function2

Agency 4  Function3 etc

 

Is there a more elegant /efficient way other than what I did which was to
separate Col2 into separate columns and then gather the data back up

 

myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2", "x3",
"x4", "x5"), sep = ",")  

myDat4 <- gather(data=myDat3, key="type", value = "Value",
"x1","x2","x3","x4","x5", na.rm = TRUE)

 

while it works, looking for a more elegant solution, if there is one

 

Jeff


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] vectorizing the integrate function

2019-08-12 Thread ravi via R-help
 Hi all,I would like to thank every one who has responded to my question. I 
have received valuable help.Linus, the function that I gave here is just a toy 
function. I think that is possible even to find an analytical solution. I chose 
that just to include many constant parameters. I wanted to find out how I can 
vectorize. I have obtained nice answers from Bert and I appreciate the help. I 
knew even previously that the vectorizing primarily improves the readability of 
the code. Thanks Bert for making it crystal clear that the looping is still at 
the interpreted level of R.Thanks,Ravi
On Sunday, 11 August 2019, 12:24:43 CEST, Linus Chen 
 wrote:  
 
 Dear ravi,

In your example, the function "f" is linear to a, b, and c.
Is this the general case in your task?

If it is, you can save  *lots* of computation taking advantage of the linearity.

Lei


On Sat, 10 Aug 2019 at 19:20, ravi via R-help  wrote:
>
> Hi all,I am having some difficulties in vectorizing the integrate function. 
> Let me explain with an example.
> a <- 10; b <- 3; c <- 4
> f <- function(x) {exp(-a*x^3-b*x^2-c*x)}
> integrate(f,0,Inf) # works fine
>
> My difficulties start when I want to vectorize.
>
> # attempts to vectorize fail
> a <- seq(from=0,to=1,by=0.5)
> b <- seq(from=5,to=10,by=1)
> m <- seq(from=10,to=20,by=5)
> f2 <- function(x,a,b,c) {exp(-a*x^3-b*x^2-m*x)}
> fv <- 
> Vectorize(integrate(f2,0,Inf),vectorize.args=c("a","b","m"),SIMPLIFY=TRUE)
>
> I want the result as a 3-d array with dimensions of the lengths of a, b and 
> c. I have tried several variants but am not having much luck. Will appreciate 
> any help that I can get.
> Thanks,Ravi Sutradhara
>
>
>
>
>        [[alternative HTML version deleted]]
>
> __
> R-help@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]]

__
R-help@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.