Is there a means to split a vector into its individual elements without going the brute-force route for arguments to a predefined function call? offred.rgb <- c(1, 0, 0) * 0.60; ## Brute force style offred.col <- rgb(offred.rgb[1], offred.rgb[2], offred.rgb[3], names = "offred") ## Desired style offred.col <- rgb(silver.bullet(offred.rgb), names = "offred") Neither of my attempts gets it right. silver.bullet.try1 <- function(x) { expr <- cat(x, sep = ",") return(parse(text = expr)) } silver.bullet.try2 <- function(x) { expr <- expression(cat(x, sep = ",")) return(eval(expr)) } ---------------------------------------------------------- SIGSIG -- signature too long (core dumped)
do.call() is good for this, I believe:> offred.rgb <- c(1, 0, 0) * 0.60 > offred.col <- do.call("rgb", c(as.list(offred.rgb), names="offred")) > offred.col[1] "#990000" HTH, Andy> From: Paul Roebuck > > Is there a means to split a vector into its individual > elements without going the brute-force route for arguments > to a predefined function call? > > offred.rgb <- c(1, 0, 0) * 0.60; > > ## Brute force style > offred.col <- rgb(offred.rgb[1], > offred.rgb[2], > offred.rgb[3], > names = "offred") > ## Desired style > offred.col <- rgb(silver.bullet(offred.rgb), > names = "offred") > > Neither of my attempts gets it right. > > silver.bullet.try1 <- function(x) { > expr <- cat(x, sep = ",") > return(parse(text = expr)) > } > > silver.bullet.try2 <- function(x) { > expr <- expression(cat(x, sep = ",")) > return(eval(expr)) > } > > ---------------------------------------------------------- > SIGSIG -- signature too long (core dumped) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Have you considered "do.call": > do.call("rgb", as.list((1:3)/10)) [1] "#1A334C" same as: > rgb(.1, .2, .3) [1] "#1A334C" Hope this helps. spencer graves Paul Roebuck wrote:>Is there a means to split a vector into its individual >elements without going the brute-force route for arguments >to a predefined function call? > > offred.rgb <- c(1, 0, 0) * 0.60; > > ## Brute force style > offred.col <- rgb(offred.rgb[1], > offred.rgb[2], > offred.rgb[3], > names = "offred") > ## Desired style > offred.col <- rgb(silver.bullet(offred.rgb), > names = "offred") > >Neither of my attempts gets it right. > > silver.bullet.try1 <- function(x) { > expr <- cat(x, sep = ",") > return(parse(text = expr)) > } > > silver.bullet.try2 <- function(x) { > expr <- expression(cat(x, sep = ",")) > return(eval(expr)) > } > >---------------------------------------------------------- >SIGSIG -- signature too long (core dumped) > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567
Dear Paul, How about do.call("rgb", as.list(offred.rgb)) ? I hope that this helps, John On Wed, 15 Sep 2004 15:20:24 -0500 (CDT) Paul Roebuck <roebuck at odin.mdacc.tmc.edu> wrote:> Is there a means to split a vector into its individual > elements without going the brute-force route for arguments > to a predefined function call? > > offred.rgb <- c(1, 0, 0) * 0.60; > > ## Brute force style > offred.col <- rgb(offred.rgb[1], > offred.rgb[2], > offred.rgb[3], > names = "offred") > ## Desired style > offred.col <- rgb(silver.bullet(offred.rgb), > names = "offred") > > Neither of my attempts gets it right. > > silver.bullet.try1 <- function(x) { > expr <- cat(x, sep = ",") > return(parse(text = expr)) > } > > silver.bullet.try2 <- function(x) { > expr <- expression(cat(x, sep = ",")) > return(eval(expr)) > } > > ---------------------------------------------------------- > SIGSIG -- signature too long (core dumped) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/
Paul Roebuck <roebuck <at> odin.mdacc.tmc.edu> writes: : Is there a means to split a vector into its individual : elements without going the brute-force route for arguments : to a predefined function call? : : offred.rgb <- c(1, 0, 0) * 0.60; : : ## Brute force style : offred.col <- rgb(offred.rgb[1], : offred.rgb[2], : offred.rgb[3], : names = "offred") : ## Desired style : offred.col <- rgb(silver.bullet(offred.rgb), : names = "offred") See: http://maths.newcastle.edu.au/~rking/R/help/03a/7417.html
Paul Roebuck <roebuck at odin.mdacc.tmc.edu> writes:> Is there a means to split a vector into its individual > elements without going the brute-force route for arguments > to a predefined function call? > > offred.rgb <- c(1, 0, 0) * 0.60; > > ## Brute force style > offred.col <- rgb(offred.rgb[1], > offred.rgb[2], > offred.rgb[3], > names = "offred") > ## Desired style > offred.col <- rgb(silver.bullet(offred.rgb), > names = "offred")The closest is probably this: offred.col <- do.call("rgb", c(as.list(offred.rgb), list(names="offred"))) (ever read/seen The Handmaid's Tale, btw?) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
> From: Paul Roebuck > > On Wed, 15 Sep 2004, Peter Dalgaard wrote: > > > Paul Roebuck <roebuck at odin.mdacc.tmc.edu> writes: > > > > > Is there a means to split a vector into its individual > > > elements without going the brute-force route for arguments > > > to a predefined function call? > > > > > > offred.rgb <- c(1, 0, 0) * 0.60; > > > > > > ## Brute force style > > > offred.col <- rgb(offred.rgb[1], > > > offred.rgb[2], > > > offred.rgb[3], > > > names = "offred") > > > ## Desired style > > > offred.col <- rgb(silver.bullet(offred.rgb), > > > names = "offred") > > > > The closest is probably this: > > > > offred.col <- do.call("rgb", c(as.list(offred.rgb), > > list(names="offred"))) > > Everyone offered 'do.call' as the solution. While that > works, is it to say that there is no means of expanding > the expression as an argument to the original function?What would be the point? That's what do.call() does for you internally. Andy> > (ever read/seen The Handmaid's Tale, btw?) > > > > Not yet. Though renaming my sample variable 'off.red.col' > would avoid future confusion with oppressed handmaids. > > ---------------------------------------------------------- > SIGSIG -- signature too long (core dumped) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
> From: Liaw, Andy > > > From: Paul Roebuck > > > > On Wed, 15 Sep 2004, Peter Dalgaard wrote: > > > > > Paul Roebuck <roebuck at odin.mdacc.tmc.edu> writes: > > > > > > > Is there a means to split a vector into its individual > > > > elements without going the brute-force route for arguments > > > > to a predefined function call? > > > > > > > > offred.rgb <- c(1, 0, 0) * 0.60; > > > > > > > > ## Brute force style > > > > offred.col <- rgb(offred.rgb[1], > > > > offred.rgb[2], > > > > offred.rgb[3], > > > > names = "offred") > > > > ## Desired style > > > > offred.col <- rgb(silver.bullet(offred.rgb), > > > > names = "offred") > > > > > > The closest is probably this: > > > > > > offred.col <- do.call("rgb", c(as.list(offred.rgb), > > > list(names="offred"))) > > > > Everyone offered 'do.call' as the solution. While that > > works, is it to say that there is no means of expanding > > the expression as an argument to the original function? > > What would be the point? That's what do.call() does for you > internally.Is this what you're after?> toCall <- c(as.name("rgb"), as.list(offred.rgb), names="offred") > toCall[[1]] rgb [[2]] [1] 0.6 [[3]] [1] 0 [[4]] [1] 0 $names [1] "offred"> toCall <- as.call(toCall) > toCallrgb(0.6, 0, 0, names = "offred")> eval(toCall)[1] "#990000" Andy> Andy > > > > (ever read/seen The Handmaid's Tale, btw?) > > > > > > > Not yet. Though renaming my sample variable 'off.red.col' > > would avoid future confusion with oppressed handmaids. > > > > ---------------------------------------------------------- > > SIGSIG -- signature too long (core dumped) > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.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://stat.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. > -------------------------------------------------------------- > ---------------- >