Sebastien Bihorel
2019-Jul-03 07:15 UTC
[R] Control the variable order after multiple declarations using within
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
Kevin Thorpe
2019-Jul-03 12:11 UTC
[R] Control the variable order after multiple declarations using within
> On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel <sebastien.bihorel at 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 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.-- 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.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016
Eric Berger
2019-Jul-03 12:13 UTC
[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 < sebastien.bihorel at 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 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]]
Sebastien Bihorel
2019-Jul-03 12:23 UTC
[R] Control the variable order after multiple declarations using within
Hi Eric, I was hoping to avoid post-processing the result of the within call. Sebastien From: "Eric Berger" <ericjberger at gmail.com> To: "Sebastien Bihorel" <sebastien.bihorel at cognigencorp.com> Cc: "R mailing list" <r-help at 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.bihorel at cognigencorp.com | sebastien.bihorel at 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 at r-project.org | R-help at 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]]
Sebastien Bihorel
2019-Jul-03 12:24 UTC
[R] Control the variable order after multiple declarations using within
Hi Kevin, I was hoping to stay within base R functionality. Thanks ----- Original Message ----- From: "Kevin Thorpe" <kevin.thorpe at utoronto.ca> To: "Sebastien Bihorel" <sebastien.bihorel at cognigencorp.com> Cc: "R Help Mailing List" <r-help at r-project.org> 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 <sebastien.bihorel at 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 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.-- 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.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016
Richard O'Keefe
2019-Jul-03 13:27 UTC
[R] Control the variable order after multiple declarations using within
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 <kevin.thorpe at utoronto.ca> wrote:> > > On Jul 3, 2019, at 3:15 AM, Sebastien Bihorel < > sebastien.bihorel at 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 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. > > > -- > 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.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016 > > ______________________________________________ > 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]]