Hi Eric, 

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

Sebastien 


From: "Eric Berger" <ericjber...@gmail.com> 
To: "Sebastien Bihorel" <sebastien.biho...@cognigencorp.com> 
Cc: "R mailing list" <r-help@r-project.org> 
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.

Reply via email to