Hello list, I have two vectors x and x2: x=runif(10) x2=runif(10) and one vectors with their names : my.names=c("x","x2") I would like to cbind these two vectors using their names contained in the vector my.names. I can create a string with comma ncomma=paste(my.names,collapse=",") and now, I just need a function to transform this string into a adequate argument for cbind: cbind(afunction(ncomma)) Is there in R a function that can do the job ? If not, how can I do it ?? Thanks in advance, Sincerely. St?phane DRAY -------------------------------------------------------------------------------------------------- D?partement des Sciences Biologiques Universit? de Montr?al, C.P. 6128, succursale centre-ville Montr?al, Qu?bec H3C 3J7, Canada Tel : 514 343 6111 poste 1233 E-mail : stephane.dray at umontreal.ca -------------------------------------------------------------------------------------------------- Web http://www.steph280.freesurf.fr/
?get to convert names into objects> -----Original Message----- > From: Stephane DRAY [mailto:stephane.dray at umontreal.ca] > Sent: Wednesday, March 24, 2004 11:41 AM > To: r-help at stat.math.ethz.ch > Subject: [R] binding vectors or matrix using their names > > > Hello list, > I have two vectors x and x2: > > x=runif(10) > x2=runif(10) > > and one vectors with their names : > > my.names=c("x","x2") > > I would like to cbind these two vectors using their names > contained in the > vector my.names. > I can create a string with comma > ncomma=paste(my.names,collapse=",") > > and now, I just need a function to transform this string into > a adequate > argument for cbind: > > cbind(afunction(ncomma)) > > Is there in R a function that can do the job ? If not, how > can I do it ?? > > Thanks in advance, > Sincerely. > > > St?phane DRAY > -------------------------------------------------------------- > ------------------------------------ > > D?partement des Sciences Biologiques > Universit? de Montr?al, C.P. 6128, succursale centre-ville > Montr?al, Qu?bec H3C 3J7, Canada > > Tel : 514 343 6111 poste 1233 > E-mail : stephane.dray at umontreal.ca > -------------------------------------------------------------- > ------------------------------------ > > Web > http://www.steph280.freesurf.fr/ > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help > PLEASE > do read the posting guide! > http://www.R-project.org/posting-guide.html >
I believe the syntax is result <- do.call(cbind, as.list(my.names)) Haven't checked this on your example, though. - tom blackwell - u michigan medical school - ann arbor - On Wed, 24 Mar 2004, Stephane DRAY wrote:> Hello list, > I have two vectors x and x2: > > x=runif(10) > x2=runif(10) > > and one vectors with their names : > > my.names=c("x","x2") > > I would like to cbind these two vectors using their names contained in the > vector my.names. > I can create a string with comma > ncomma=paste(my.names,collapse=",") > > and now, I just need a function to transform this string into a adequate > argument for cbind: > > cbind(afunction(ncomma)) > > Is there in R a function that can do the job ? If not, how can I do it ?? > > Thanks in advance, > Sincerely. > > > St?phane DRAY > -------------------------------------------------------------------------------------------------- > > D?partement des Sciences Biologiques > Universit? de Montr?al, C.P. 6128, succursale centre-ville > Montr?al, Qu?bec H3C 3J7, Canada > > Tel : 514 343 6111 poste 1233 > E-mail : stephane.dray at umontreal.ca > -------------------------------------------------------------------------------------------------- > > Web http://www.steph280.freesurf.fr/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Hi Tom, Your approach did not work, > do.call("cbind", as.list(my.names)) [,1] [,2] [1,] "x" "x2" but it helps me a lot to find the good one: do.call("cbind", as.list(parse(text=my.names))) Thanks, At 14:56 24/03/2004, Tom Blackwell wrote:>I believe the syntax is > >result <- do.call(cbind, as.list(my.names)) > >Haven't checked this on your example, though. > >- tom blackwell - u michigan medical school - ann arbor - > >On Wed, 24 Mar 2004, Stephane DRAY wrote: > > > Hello list, > > I have two vectors x and x2: > > > > x=runif(10) > > x2=runif(10) > > > > and one vectors with their names : > > > > my.names=c("x","x2") > > > > I would like to cbind these two vectors using their names contained in the > > vector my.names. > > I can create a string with comma > > ncomma=paste(my.names,collapse=",") > > > > and now, I just need a function to transform this string into a adequate > > argument for cbind: > > > > cbind(afunction(ncomma)) > > > > Is there in R a function that can do the job ? If not, how can I do it ?? > > > > Thanks in advance, > > Sincerely. > > > > > > St?phane DRAY > > > -------------------------------------------------------------------------------------------------- > > > > D?partement des Sciences Biologiques > > Universit? de Montr?al, C.P. 6128, succursale centre-ville > > Montr?al, Qu?bec H3C 3J7, Canada > > > > Tel : 514 343 6111 poste 1233 > > E-mail : stephane.dray at umontreal.ca > > > -------------------------------------------------------------------------------------------------- > > > > > Web http://www.steph280.freesurf.fr/ > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlSt?phane DRAY -------------------------------------------------------------------------------------------------- D?partement des Sciences Biologiques Universit? de Montr?al, C.P. 6128, succursale centre-ville Montr?al, Qu?bec H3C 3J7, Canada Tel : 514 343 6111 poste 1233 E-mail : stephane.dray at umontreal.ca -------------------------------------------------------------------------------------------------- Web http://www.steph280.freesurf.fr/
Perhaps simler:> x1 <- 1:5 > x2 <- 2:7 > xname <- c("x1", "x2") > sapply(xname, get)x1 x2 [1,] 1 2 [2,] 2 3 [3,] 3 4 [4,] 4 5 [5,] 5 6 HTH, Andy> From: Stephane DRAY > > Hi Tom, > > Your approach did not work, > > > do.call("cbind", as.list(my.names)) > [,1] [,2] > [1,] "x" "x2" > > but it helps me a lot to find the good one: > > do.call("cbind", as.list(parse(text=my.names))) > > Thanks, > > > At 14:56 24/03/2004, Tom Blackwell wrote: > > >I believe the syntax is > > > >result <- do.call(cbind, as.list(my.names)) > > > >Haven't checked this on your example, though. > > > >- tom blackwell - u michigan medical school - ann arbor - > > > >On Wed, 24 Mar 2004, Stephane DRAY wrote: > > > > > Hello list, > > > I have two vectors x and x2: > > > > > > x=runif(10) > > > x2=runif(10) > > > > > > and one vectors with their names : > > > > > > my.names=c("x","x2") > > > > > > I would like to cbind these two vectors using their names > contained in the > > > vector my.names. > > > I can create a string with comma > > > ncomma=paste(my.names,collapse=",") > > > > > > and now, I just need a function to transform this string > into a adequate > > > argument for cbind: > > > > > > cbind(afunction(ncomma)) > > > > > > Is there in R a function that can do the job ? If not, > how can I do it ?? > > > > > > Thanks in advance, > > > Sincerely. > > > > > > > > > St?phane DRAY > > > > > > -------------------------------------------------------------- > ------------------------------------ > > > > > > D?partement des Sciences Biologiques > > > Universit? de Montr?al, C.P. 6128, succursale centre-ville > > > Montr?al, Qu?bec H3C 3J7, Canada > > > > > > Tel : 514 343 6111 poste 1233 > > > E-mail : stephane.dray at umontreal.ca > > > > > > -------------------------------------------------------------- > ------------------------------------ > > > > > > > > Web > http://www.steph280.freesurf.fr/ > > > > > > > ______________________________________________ > > > R-help at stat.math.ethz.ch mailing list > > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > > > >______________________________________________ > >R-help at stat.math.ethz.ch mailing list > >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > St?phane DRAY > > -------------------------------------------------------------- > ------------------------------------ > > D?partement des Sciences Biologiques > Universit? de Montr?al, C.P. 6128, succursale centre-ville > Montr?al, Qu?bec H3C 3J7, Canada > > Tel : 514 343 6111 poste 1233 > E-mail : stephane.dray at umontreal.ca > -------------------------------------------------------------- > ------------------------------------ > > Web > http://www.steph280.freesurf.fr/ > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Gefore anyone jumps on me, I fibbed:> From: Liaw, Andy > > Perhaps simler: > > > x1 <- 1:5 > > x2 <- 2:7That should've been x2 <- 2:6. (I mistyped the first time, but cut-and-pasted the wrong line...)> > xname <- c("x1", "x2") > > sapply(xname, get) > x1 x2 > [1,] 1 2 > [2,] 2 3 > [3,] 3 4 > [4,] 4 5 > [5,] 5 6 > > HTH, > Andy > > > From: Stephane DRAY > > > > Hi Tom, > > > > Your approach did not work, > > > > > do.call("cbind", as.list(my.names)) > > [,1] [,2] > > [1,] "x" "x2" > > > > but it helps me a lot to find the good one: > > > > do.call("cbind", as.list(parse(text=my.names))) > > > > Thanks, > > > > > > At 14:56 24/03/2004, Tom Blackwell wrote: > > > > >I believe the syntax is > > > > > >result <- do.call(cbind, as.list(my.names)) > > > > > >Haven't checked this on your example, though. > > > > > >- tom blackwell - u michigan medical school - ann arbor - > > > > > >On Wed, 24 Mar 2004, Stephane DRAY wrote: > > > > > > > Hello list, > > > > I have two vectors x and x2: > > > > > > > > x=runif(10) > > > > x2=runif(10) > > > > > > > > and one vectors with their names : > > > > > > > > my.names=c("x","x2") > > > > > > > > I would like to cbind these two vectors using their names > > contained in the > > > > vector my.names. > > > > I can create a string with comma > > > > ncomma=paste(my.names,collapse=",") > > > > > > > > and now, I just need a function to transform this string > > into a adequate > > > > argument for cbind: > > > > > > > > cbind(afunction(ncomma)) > > > > > > > > Is there in R a function that can do the job ? If not, > > how can I do it ?? > > > > > > > > Thanks in advance, > > > > Sincerely. > > > > > > > > > > > > St?phane DRAY > > > > > > > > > -------------------------------------------------------------- > > ------------------------------------ > > > > > > > > D?partement des Sciences Biologiques > > > > Universit? de Montr?al, C.P. 6128, succursale centre-ville > > > > Montr?al, Qu?bec H3C 3J7, Canada > > > > > > > > Tel : 514 343 6111 poste 1233 > > > > E-mail : stephane.dray at umontreal.ca > > > > > > > > > -------------------------------------------------------------- > > ------------------------------------ > > > > > > > > > > > Web > > http://www.steph280.freesurf.fr/ > > > > > > > > > > ______________________________________________ > > > > R-help at stat.math.ethz.ch mailing list > > > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > > PLEASE do read the posting guide! > > > http://www.R-project.org/posting-guide.html > > > > > > > > > >______________________________________________ > > >R-help at stat.math.ethz.ch mailing list > > >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > >PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > St?phane DRAY > > > > -------------------------------------------------------------- > > ------------------------------------ > > > > D?partement des Sciences Biologiques > > Universit? de Montr?al, C.P. 6128, succursale centre-ville > > Montr?al, Qu?bec H3C 3J7, Canada > > > > Tel : 514 343 6111 poste 1233 > > E-mail : stephane.dray at umontreal.ca > > -------------------------------------------------------------- > > ------------------------------------ > > > > Web > > http://www.steph280.freesurf.fr/ > > > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > -------------------------------------------------------------- > ---------------- > Notice: This e-mail message, together with any attachments, > contains information of Merck & Co., Inc. (One Merck Drive, > Whitehouse Station, New Jersey, USA 08889), and/or its > affiliates (which may be known outside the United States as > Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as > Banyu) that may be confidential, proprietary copyrighted > and/or legally privileged. It is intended solely for the use > of the individual or entity named on this message. If you > are not the intended recipient, and have received this > message in error, please notify us immediately by reply > e-mail and then delete it from your system. > -------------------------------------------------------------- > ---------------- >
Bill.Venables@csiro.au
2004-Mar-25 02:10 UTC
[R] binding vectors or matrix using their names
Goodness Patrick, this must surely qualify for the obfuscated R competition finals. I love it! There are two solutions I can think of with do.call and here they are:> x <- 1 > x2 <- runif(10) > x12 <- c("x", "x2")> do.call("cbind", lapply(x12, as.name))x x2 [1,] 1 0.99327265 [2,] 1 0.63260097 [3,] 1 0.17411170 [4,] 1 0.54635634 [5,] 1 0.75603670 [6,] 1 0.27739270 [7,] 1 0.32125068 [8,] 1 0.01326344 [9,] 1 0.37519602 [10,] 1 0.11133052> do.call("cbind", lapply(x12, get))[,1] [,2] [1,] 1 0.99327265 [2,] 1 0.63260097 [3,] 1 0.17411170 [4,] 1 0.54635634 [5,] 1 0.75603670 [6,] 1 0.27739270 [7,] 1 0.32125068 [8,] 1 0.01326344 [9,] 1 0.37519602 [10,] 1 0.11133052>I suspect the first offers a few advantages over the second, (which some other people have implicitly suggested). The first preserves the names in the result. Also, if the vectors are large, the second constructs a large language object and then evaluates it to get a large result. The first uses the names rather than the objects themselves, so at least the language object is small, even if the result is not. A more serious, philosophical word on Patrick's solution. It is rarely necessary (in my limited experience, sure) to have to use parse() like this. Where it provides a quick (kludgy?) solution I often find it a useful exercise to consider alternatives. They often come out simpler and you nearly always pick up something useful in the process. Bill V. -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Patrick Burns Sent: Thursday, 25 March 2004 7:02 AM To: Stephane DRAY Cc: r-help at stat.math.ethz.ch; Stephane DRAY; Tom Blackwell Subject: Re: [R] binding vectors or matrix using their names I think you are looking for the eval-parse-text idiom: eval(parse(text=paste("cbind(", paste(my.names, collapse=", "), ")"))) Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Stephane DRAY wrote:> Hi Tom, > > Your approach did not work, > > > do.call("cbind", as.list(my.names)) > [,1] [,2] > [1,] "x" "x2" > > but it helps me a lot to find the good one: > > do.call("cbind", as.list(parse(text=my.names))) > > Thanks, > > > At 14:56 24/03/2004, Tom Blackwell wrote: > >> I believe the syntax is >> >> result <- do.call(cbind, as.list(my.names)) >> >> Haven't checked this on your example, though. >> >> - tom blackwell - u michigan medical school - ann arbor - >> >> On Wed, 24 Mar 2004, Stephane DRAY wrote: >> >> > Hello list, >> > I have two vectors x and x2: >> > >> > x=runif(10) >> > x2=runif(10) >> > >> > and one vectors with their names : >> > >> > my.names=c("x","x2") >> > >> > I would like to cbind these two vectors using their names contained >> in the >> > vector my.names. >> > I can create a string with comma >> > ncomma=paste(my.names,collapse=",") >> > >> > and now, I just need a function to transform this string into a >> adequate >> > argument for cbind: >> > >> > cbind(afunction(ncomma)) >> > >> > Is there in R a function that can do the job ? If not, how can I do >> it ?? >> > >> > Thanks in advance, >> > Sincerely. >> > >> > >> > St?phane DRAY >> > >> --------------------------------------------------------------------- >> ----------------------------- >> >> > >> > D?partement des Sciences Biologiques >> > Universit? de Montr?al, C.P. 6128, succursale centre-ville >> > Montr?al, Qu?bec H3C 3J7, Canada >> > >> > Tel : 514 343 6111 poste 1233 >> > E-mail : stephane.dray at umontreal.ca >> > >> --------------------------------------------------------------------- >> ----------------------------- >> >> > >> > Web >> http://www.steph280.freesurf.fr/ >> > >> > ______________________________________________ >> > R-help at stat.math.ethz.ch mailing list >> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guide! >> http://www.R-project.org/posting-guide.html >> > >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://www.stat.math.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! >> http://www.R-project.org/posting-guide.html > > > St?phane DRAY > ---------------------------------------------------------------------- > ---------------------------- > > D?partement des Sciences Biologiques > Universit? de Montr?al, C.P. 6128, succursale centre-ville Montr?al, > Qu?bec H3C 3J7, Canada > > Tel : 514 343 6111 poste 1233 > E-mail : stephane.dray at umontreal.ca > ---------------------------------------------------------------------- > ---------------------------- > > Web > http://www.steph280.freesurf.fr/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Well, I ***liked*** Patrick's approach. Why? Because it's basically straightforward. You put together a character string by pasting together the names of the objects you want to do things to, and the things (operations) you want to do to them. Then you get R to ``execute'' this string just as if you had typed it in to the keyboard. This seems an obvious and sensible approach to the problem and is completely generalizable. There is no obfuscation (making things obscure) involved. The solutions involving do.call() are much more ***elegant*** and doubtless far less prone to pitfalls. However they are far less obvious and thereby much more obscure, so it is the do.call() solutions that deserve the ``obfuscation'' descriptor if anything does. cheers, Rolf Turner rolf at math.unb.ca