RE: [R] Splitting vector into individual elements
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] #99 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) __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Splitting vector into individual elements
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) __ [EMAIL PROTECTED] 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 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Splitting vector into individual elements
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 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Splitting vector into individual elements
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 [EMAIL PROTECTED] 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) __ [EMAIL PROTECTED] 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/ __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Splitting vector into individual elements
Paul Roebuck [EMAIL PROTECTED] 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 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Splitting vector into individual elements
On Wed, 15 Sep 2004, Peter Dalgaard wrote: Paul Roebuck [EMAIL PROTECTED] 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? (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) __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] Splitting vector into individual elements
From: Paul Roebuck On Wed, 15 Sep 2004, Peter Dalgaard wrote: Paul Roebuck [EMAIL PROTECTED] 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) __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Splitting vector into individual elements
Paul Roebuck [EMAIL PROTECTED] writes: 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? Not really. You need an explicit expansion of the argument to a list somehow, and there's no silver bullet that can convert one function argument to several. There are solutions without do.call, like offred.rgb - c(1, 0, 0) * 0.60 x - quote(rgb(.,.,.,names=offred)) x[2:4] - as.list(offred.rgb) eval(x) offred #99 but you might find it difficult to explain how it works a year later -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] Splitting vector into individual elements
From: Liaw, Andy From: Paul Roebuck On Wed, 15 Sep 2004, Peter Dalgaard wrote: Paul Roebuck [EMAIL PROTECTED] 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) toCall rgb(0.6, 0, 0, names = offred) eval(toCall) [1] #99 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) __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] 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. -- __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Splitting vector into individual elements
Slightly more transparent but arguably uglier: offred.rgb - c(1, 0, 0) * 0.60 ofr - paste(offred.rgb, collapse=,) ofr. - paste(rgb(, ofr, ',names=offred)') ofr. [1] rgb( 0.6,0,0 ,names=\offred\) eval(parse(text=ofr.)) offred #99 As long as I can remember eval(parse(text=, this is for me the most transparent and works for constructing virtually any R command. hope this helps. spencer graves Peter Dalgaard wrote: Paul Roebuck [EMAIL PROTECTED] writes: 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? Not really. You need an explicit expansion of the argument to a list somehow, and there's no silver bullet that can convert one function argument to several. There are solutions without do.call, like offred.rgb - c(1, 0, 0) * 0.60 x - quote(rgb(.,.,.,names=offred)) x[2:4] - as.list(offred.rgb) eval(x) offred #99 but you might find it difficult to explain how it works a year later -- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Splitting vector into individual elements
Gabor Grothendieck ggrothendieck at myway.com writes: : : Paul Roebuck roebuck at odin.mdacc.tmc.edu writes: : : : : : 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? : : This is not a true answer to the question of expanding a list : into arguments without using do.call but it does allow you to : carry out either syntax in this particular case using S3 : dispatch: : : R silver.bullet - as.list : R rgb - function(x, ...) UseMethod(rgb) : R rgb.list - function(x, ...) rgb(x[[1]],x[[2]],x[[3]],...) : R rgb.default - graphics::rgb : : R offred.rgb - c(1, 0, 0) * 0.60; : R # original syntax : R rgb(offred.rgb[1], offred.rgb[2], offred.rgb[3], names = offred) :offred : #99 : R # list syntax : R rgb(silver.bullet(offred.rgb), names = offred) :offred : #99 Here is a second, different approach. Again, it is not exactly what you are asking for since silver.bullet, here called flatten.args, is applied to the function rather than the argument in question and operates on all arguments, not just one but I think its closer in spirit to your query than my previous solution: flatten.args - function(f) function(...) { L - list() for(i in list(...)) L - c(L, unlist(i)) do.call(as.character(substitute(f)), L) } flatten.args(rgb)(0.6 * c(1,0,0), name = offred) __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html