Re: [R] Control the variable order after multiple declarations using within

2019-07-04 Thread Sebastien Bihorel
Thanks all for your inputs.

- Original Message -
From: "Duncan Murdoch" 
To: "Jeff Newmiller" , r-help@r-project.org, "Eric 
Berger" , "Richard O'Keefe" 
Cc: "Sebastien Bihorel" 
Sent: Wednesday, July 3, 2019 12:52:55 PM
Subject: Re: [R] Control the variable order after multiple declarations using 
within

On 03/07/2019 12:42 p.m., Jeff Newmiller wrote:
> Dummy columns do have some drawbacks though, if you find yourself working 
> with large data frames. The dummy columns waste memory and time as compared 
> to either reorganizing columns after the `within` or using separate 
> sequential `with` expressions as I previously suggested. I think mutate 
> avoids this overhead also.

I think mutate() has only a very small advantage over within().  Neither 
one of them is flexible about the order of columns in the final result. 
In the OPs example, mutate creates the variables in the desired order, 
but it would be no better if the desired order had been a, c, b, because 
b is needed for the calculation of c, so it would be created first.

Eric's suggestion

within(df, {b<-a*2; c<-b*3})[c("a","b","c")]

is the best so far, though I'd probably write it as

within(df, {b<-a*2; c<-b*3})[, c("a","b","c")]

just to avoid confusing my future self and make clear that I'm talking 
about specifying an order for the columns.

And if you really, really want everything to happen within the call, 
just create the variables in the reverse order to what you want, e.g.

within(df, {c <- a; b<-a*2; c<-b*3})

but to me that is a lot less clear than Eric's solution.

Duncan Murdoch


> 
> On July 3, 2019 8:25:32 AM PDT, Eric Berger  wrote:
>> Nice suggestion, Richard.
>>
>> On Wed, Jul 3, 2019 at 4:28 PM Richard O'Keefe 
>> wrote:
>>
>>> Why not set all the new columns to dummy values to get the order you
>>> want and then set them to their final values in the order that works
>>> for that?
>>>
>>>
>>> On Thu, 4 Jul 2019 at 00:12, Kevin Thorpe 
>>> wrote:
>>>
>>>>
>>>>> On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel <
>>>> sebastien.biho...@cognigencorp.com> wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> The within function can be used to modify data.frames (among
>> other
>>>> objects). One can even provide multiple expressions to modify the
>>>> data.frame by more than one expression. However, when new variables
>> are
>>>> created, they seem to be inserted in the data.frame in the opposite
>> order
>>>> they were declared:
>>>>>
>>>>>> df <- data.frame(a=1)
>>>>>> within(df, {b<-a*2; c<-b*3})
>>>>>   a c b
>>>>> 1 1 6 2
>>>>>
>>>>> Is there a way to insert the variables in an order consistent
>> with the
>>>> order of declaration (ie, a, b, c)?
>>>>>
>>>>
>>>> One way is to use mutate() from the dplyr package.
>>>>
>>>>
>>>>> Thanks
>>>>>
>>>>> Sebastien
>>>>>
>>>>> __
>>>>> 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.
>>>>
>>>>
>>>> --
>>>> Kevin E. Thorpe
>>>> Head of Biostatistics,  Applied Health Research Centre (AHRC)
>>>> Li Ka Shing Knowledge Institute of St. Michael's
>>>> Assistant Professor, Dalla Lana School of Public Health
>>>> University of Toronto
>>>> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax:
>> 416.864.3016
>>>>
>>>> __
>>>> 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.
>>>
>>
>>  [[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] Control the variable order after multiple declarations using within

2019-07-03 Thread Duncan Murdoch

On 03/07/2019 12:42 p.m., Jeff Newmiller wrote:

Dummy columns do have some drawbacks though, if you find yourself working with 
large data frames. The dummy columns waste memory and time as compared to 
either reorganizing columns after the `within` or using separate sequential 
`with` expressions as I previously suggested. I think mutate avoids this 
overhead also.


I think mutate() has only a very small advantage over within().  Neither 
one of them is flexible about the order of columns in the final result. 
In the OPs example, mutate creates the variables in the desired order, 
but it would be no better if the desired order had been a, c, b, because 
b is needed for the calculation of c, so it would be created first.


Eric's suggestion

within(df, {b<-a*2; c<-b*3})[c("a","b","c")]

is the best so far, though I'd probably write it as

within(df, {b<-a*2; c<-b*3})[, c("a","b","c")]

just to avoid confusing my future self and make clear that I'm talking 
about specifying an order for the columns.


And if you really, really want everything to happen within the call, 
just create the variables in the reverse order to what you want, e.g.


within(df, {c <- a; b<-a*2; c<-b*3})

but to me that is a lot less clear than Eric's solution.

Duncan Murdoch




On July 3, 2019 8:25:32 AM PDT, Eric Berger  wrote:

Nice suggestion, Richard.

On Wed, Jul 3, 2019 at 4:28 PM Richard O'Keefe 
wrote:


Why not set all the new columns to dummy values to get the order you
want and then set them to their final values in the order that works
for that?


On Thu, 4 Jul 2019 at 00:12, Kevin Thorpe 
wrote:




On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel <

sebastien.biho...@cognigencorp.com> wrote:


Hi,

The within function can be used to modify data.frames (among

other

objects). One can even provide multiple expressions to modify the
data.frame by more than one expression. However, when new variables

are

created, they seem to be inserted in the data.frame in the opposite

order

they were declared:



df <- data.frame(a=1)
within(df, {b<-a*2; c<-b*3})

  a c b
1 1 6 2

Is there a way to insert the variables in an order consistent

with the

order of declaration (ie, a, b, c)?




One way is to use mutate() from the dplyr package.



Thanks

Sebastien

__
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.



--
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax:

416.864.3016


__
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.



[[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] Control the variable order after multiple declarations using within

2019-07-03 Thread Jeff Newmiller
Dummy columns do have some drawbacks though, if you find yourself working with 
large data frames. The dummy columns waste memory and time as compared to 
either reorganizing columns after the `within` or using separate sequential 
`with` expressions as I previously suggested. I think mutate avoids this 
overhead also.

On July 3, 2019 8:25:32 AM PDT, Eric Berger  wrote:
>Nice suggestion, Richard.
>
>On Wed, Jul 3, 2019 at 4:28 PM Richard O'Keefe 
>wrote:
>
>> Why not set all the new columns to dummy values to get the order you
>> want and then set them to their final values in the order that works
>> for that?
>>
>>
>> On Thu, 4 Jul 2019 at 00:12, Kevin Thorpe 
>> wrote:
>>
>> >
>> > > On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel <
>> > sebastien.biho...@cognigencorp.com> wrote:
>> > >
>> > > Hi,
>> > >
>> > > The within function can be used to modify data.frames (among
>other
>> > objects). One can even provide multiple expressions to modify the
>> > data.frame by more than one expression. However, when new variables
>are
>> > created, they seem to be inserted in the data.frame in the opposite
>order
>> > they were declared:
>> > >
>> > >> df <- data.frame(a=1)
>> > >> within(df, {b<-a*2; c<-b*3})
>> > >  a c b
>> > > 1 1 6 2
>> > >
>> > > Is there a way to insert the variables in an order consistent
>with the
>> > order of declaration (ie, a, b, c)?
>> > >
>> >
>> > One way is to use mutate() from the dplyr package.
>> >
>> >
>> > > Thanks
>> > >
>> > > Sebastien
>> > >
>> > > __
>> > > 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.
>> >
>> >
>> > --
>> > Kevin E. Thorpe
>> > Head of Biostatistics,  Applied Health Research Centre (AHRC)
>> > Li Ka Shing Knowledge Institute of St. Michael's
>> > Assistant Professor, Dalla Lana School of Public Health
>> > University of Toronto
>> > email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax:
>416.864.3016
>> >
>> > __
>> > 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.
>>
>
>   [[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] Control the variable order after multiple declarations using within

2019-07-03 Thread Eric Berger
Nice suggestion, Richard.

On Wed, Jul 3, 2019 at 4:28 PM Richard O'Keefe  wrote:

> Why not set all the new columns to dummy values to get the order you
> want and then set them to their final values in the order that works
> for that?
>
>
> On Thu, 4 Jul 2019 at 00:12, Kevin Thorpe 
> wrote:
>
> >
> > > On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel <
> > sebastien.biho...@cognigencorp.com> wrote:
> > >
> > > Hi,
> > >
> > > The within function can be used to modify data.frames (among other
> > objects). One can even provide multiple expressions to modify the
> > data.frame by more than one expression. However, when new variables are
> > created, they seem to be inserted in the data.frame in the opposite order
> > they were declared:
> > >
> > >> df <- data.frame(a=1)
> > >> within(df, {b<-a*2; c<-b*3})
> > >  a c b
> > > 1 1 6 2
> > >
> > > Is there a way to insert the variables in an order consistent with the
> > order of declaration (ie, a, b, c)?
> > >
> >
> > One way is to use mutate() from the dplyr package.
> >
> >
> > > Thanks
> > >
> > > Sebastien
> > >
> > > __
> > > 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.
> >
> >
> > --
> > Kevin E. Thorpe
> > Head of Biostatistics,  Applied Health Research Centre (AHRC)
> > Li Ka Shing Knowledge Institute of St. Michael's
> > Assistant Professor, Dalla Lana School of Public Health
> > University of Toronto
> > email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
> >
> > __
> > 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.
>

[[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] Control the variable order after multiple declarations using within

2019-07-03 Thread Richard O'Keefe
Why not set all the new columns to dummy values to get the order you
want and then set them to their final values in the order that works
for that?


On Thu, 4 Jul 2019 at 00:12, Kevin Thorpe  wrote:

>
> > On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel <
> sebastien.biho...@cognigencorp.com> wrote:
> >
> > Hi,
> >
> > The within function can be used to modify data.frames (among other
> objects). One can even provide multiple expressions to modify the
> data.frame by more than one expression. However, when new variables are
> created, they seem to be inserted in the data.frame in the opposite order
> they were declared:
> >
> >> df <- data.frame(a=1)
> >> within(df, {b<-a*2; c<-b*3})
> >  a c b
> > 1 1 6 2
> >
> > Is there a way to insert the variables in an order consistent with the
> order of declaration (ie, a, b, c)?
> >
>
> One way is to use mutate() from the dplyr package.
>
>
> > Thanks
> >
> > Sebastien
> >
> > __
> > 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.
>
>
> --
> Kevin E. Thorpe
> Head of Biostatistics,  Applied Health Research Centre (AHRC)
> Li Ka Shing Knowledge Institute of St. Michael's
> Assistant Professor, Dalla Lana School of Public Health
> University of Toronto
> email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>
> __
> 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] Control the variable order after multiple declarations using within

2019-07-03 Thread Jeff Newmiller
I have found using `with` to create one column at a time to be more clear than 
using `within` when sticking with base R, though I don't see why `within` 
couldn't be repaired to behave more like `mutate`.

On July 3, 2019 5:24:14 AM PDT, Sebastien Bihorel 
 wrote:
>Hi Kevin,
>
>I was hoping to stay within base R functionality.
>
>Thanks
>
>- Original Message -
>From: "Kevin Thorpe" 
>To: "Sebastien Bihorel" 
>Cc: "R Help Mailing List" 
>Sent: Wednesday, July 3, 2019 8:11:51 AM
>Subject: Re: [R] Control the variable order after multiple declarations
>using within
>
>> On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel
> wrote:
>> 
>> Hi, 
>> 
>> The within function can be used to modify data.frames (among other
>objects). One can even provide multiple expressions to modify the
>data.frame by more than one expression. However, when new variables are
>created, they seem to be inserted in the data.frame in the opposite
>order they were declared: 
>> 
>>> df <- data.frame(a=1) 
>>> within(df, {b<-a*2; c<-b*3})
>>  a c b
>> 1 1 6 2 
>> 
>> Is there a way to insert the variables in an order consistent with
>the order of declaration (ie, a, b, c)? 
>> 
>
>One way is to use mutate() from the dplyr package.
>
>
>> Thanks 
>> 
>> Sebastien
>> 
>> __
>> 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] Control the variable order after multiple declarations using within

2019-07-03 Thread Sebastien Bihorel
Hi Eric, 

I was hoping to avoid post-processing the result of the within call. 

Sebastien 


From: "Eric Berger"  
To: "Sebastien Bihorel"  
Cc: "R mailing list"  
Sent: Wednesday, July 3, 2019 8:13:22 AM 
Subject: Re: [R] Control the variable order after multiple declarations using 
within 

Hi Sebastien, 
Your 'within' command returns a dataframe. So without changing the call to 
within you have some options such as: 

df2 <- within(df, {b<-a*2; c<-b*3}) 
df2[c("a","b","c")] 

OR 

within(df, {b<-a*2; c<-b*3})[c("a","b","c")] 

OR 

within(df, {b<-a*2; c<-b*3})[c(1,3,2)] 

HTH, 
Eric 




On Wed, Jul 3, 2019 at 10:14 AM Sebastien Bihorel < [ 
mailto:sebastien.biho...@cognigencorp.com | sebastien.biho...@cognigencorp.com 
] > wrote: 


Hi, 

The within function can be used to modify data.frames (among other objects). 
One can even provide multiple expressions to modify the data.frame by more than 
one expression. However, when new variables are created, they seem to be 
inserted in the data.frame in the opposite order they were declared: 

> df <- data.frame(a=1) 
> within(df, {b<-a*2; c<-b*3}) 
a c b 
1 1 6 2 

Is there a way to insert the variables in an order consistent with the order of 
declaration (ie, a, b, c)? 

Thanks 

Sebastien 

__ 
[ mailto:R-help@r-project.org | R-help@r-project.org ] mailing list -- To 
UNSUBSCRIBE and more, see 
[ https://stat.ethz.ch/mailman/listinfo/r-help | 
https://stat.ethz.ch/mailman/listinfo/r-help ] 
PLEASE do read the posting guide [ http://www.r-project.org/posting-guide.html 
| 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] Control the variable order after multiple declarations using within

2019-07-03 Thread Sebastien Bihorel
Hi Kevin,

I was hoping to stay within base R functionality.

Thanks

- Original Message -
From: "Kevin Thorpe" 
To: "Sebastien Bihorel" 
Cc: "R Help Mailing List" 
Sent: Wednesday, July 3, 2019 8:11:51 AM
Subject: Re: [R] Control the variable order after multiple declarations using 
within

> On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel 
>  wrote:
> 
> Hi, 
> 
> The within function can be used to modify data.frames (among other objects). 
> One can even provide multiple expressions to modify the data.frame by more 
> than one expression. However, when new variables are created, they seem to be 
> inserted in the data.frame in the opposite order they were declared: 
> 
>> df <- data.frame(a=1) 
>> within(df, {b<-a*2; c<-b*3})
>  a c b
> 1 1 6 2 
> 
> Is there a way to insert the variables in an order consistent with the order 
> of declaration (ie, a, b, c)? 
> 

One way is to use mutate() from the dplyr package.


> Thanks 
> 
> Sebastien
> 
> __
> 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.


-- 
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016

__
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] Control the variable order after multiple declarations using within

2019-07-03 Thread Eric Berger
Hi Sebastien,

Your 'within' command returns a dataframe. So without changing the call to
within you have some options such as:

df2 <- within(df, {b<-a*2; c<-b*3})
df2[c("a","b","c")]

OR

within(df, {b<-a*2; c<-b*3})[c("a","b","c")]

OR

within(df, {b<-a*2; c<-b*3})[c(1,3,2)]

HTH,
Eric




On Wed, Jul 3, 2019 at 10:14 AM Sebastien Bihorel <
sebastien.biho...@cognigencorp.com> wrote:

> Hi,
>
> The within function can be used to modify data.frames (among other
> objects). One can even provide multiple expressions to modify the
> data.frame by more than one expression. However, when new variables are
> created, they seem to be inserted in the data.frame in the opposite order
> they were declared:
>
> > df <- data.frame(a=1)
> > within(df, {b<-a*2; c<-b*3})
>   a c b
> 1 1 6 2
>
> Is there a way to insert the variables in an order consistent with the
> order of declaration (ie, a, b, c)?
>
> Thanks
>
> Sebastien
>
> __
> 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] Control the variable order after multiple declarations using within

2019-07-03 Thread Kevin Thorpe


> On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel 
>  wrote:
> 
> Hi, 
> 
> The within function can be used to modify data.frames (among other objects). 
> One can even provide multiple expressions to modify the data.frame by more 
> than one expression. However, when new variables are created, they seem to be 
> inserted in the data.frame in the opposite order they were declared: 
> 
>> df <- data.frame(a=1) 
>> within(df, {b<-a*2; c<-b*3})
>  a c b
> 1 1 6 2 
> 
> Is there a way to insert the variables in an order consistent with the order 
> of declaration (ie, a, b, c)? 
> 

One way is to use mutate() from the dplyr package.


> Thanks 
> 
> Sebastien
> 
> __
> 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.


-- 
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.tho...@utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016

__
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] Control the variable order after multiple declarations using within

2019-07-03 Thread Sebastien Bihorel
Hi, 

The within function can be used to modify data.frames (among other objects). 
One can even provide multiple expressions to modify the data.frame by more than 
one expression. However, when new variables are created, they seem to be 
inserted in the data.frame in the opposite order they were declared: 

> df <- data.frame(a=1) 
> within(df, {b<-a*2; c<-b*3})
  a c b
1 1 6 2 

Is there a way to insert the variables in an order consistent with the order of 
declaration (ie, a, b, c)? 

Thanks 

Sebastien

__
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.