[R] Pasting function arguments and strings
Dear R community, I am struggling a bit with a probably fairly simple task. I need to use some already existing functions as argument for a new function that I am going to create. 'dataset' is an argument, and it comprises objects named 'mean_test', 'sd_test', 'kurt_test' and so on. 'arg1' tells what object I want (mean, sd, kurt) while 'arg2' tells what to do to the object taken in 'arg1' (again could be mean, sd, but also any other operation/function). I was thinking about something like: myfunction-function(dataset,arg1,arg2) { attach(dataset) result=arg2(paste(arg1,_test)) return(result) } But this of course does not work! 'arg1' is of type closure and I cannot set it as character. Moreover, paste will create a string, and I do not think I can pass a string to the function of 'arg2'. How should I do? Thanks, Marco -- View this message in context: http://r.789695.n4.nabble.com/Pasting-function-arguments-and-strings-tp2993905p2993905.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
Re: [R] Pasting function arguments and strings
If I understand you correctly, just arg2(arg1) will work fine...One of the nice features of R. Example: f - function(dat,fun=mean,...) { fun(dat, ...) ## ... allows extra arguments to fun } f( rnorm(10), trim=.05) ## trimmed mean x - 1:10 f(x, fun = max,na.rm=TRUE) Cheers, Bert On Wed, Oct 13, 2010 at 8:57 AM, Manta mantin...@libero.it wrote: Dear R community, I am struggling a bit with a probably fairly simple task. I need to use some already existing functions as argument for a new function that I am going to create. 'dataset' is an argument, and it comprises objects named 'mean_test', 'sd_test', 'kurt_test' and so on. 'arg1' tells what object I want (mean, sd, kurt) while 'arg2' tells what to do to the object taken in 'arg1' (again could be mean, sd, but also any other operation/function). I was thinking about something like: myfunction-function(dataset,arg1,arg2) { attach(dataset) result=arg2(paste(arg1,_test)) return(result) } But this of course does not work! 'arg1' is of type closure and I cannot set it as character. Moreover, paste will create a string, and I do not think I can pass a string to the function of 'arg2'. How should I do? Thanks, Marco -- View this message in context: http://r.789695.n4.nabble.com/Pasting-function-arguments-and-strings-tp2993905p2993905.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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. -- Bert Gunter Genentech Nonclinical Biostatistics __ R-help@r-project.org mailing list 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.
Re: [R] Pasting function arguments and strings
Thaks for your quick reply Bert, although I doubt it works. The reason is that the names of the objects of the dataset, all end with the sufix '_test' and therefore I need to attach/paste/glue this suffix to the 'arg2' of the function. Any other idea? -- View this message in context: http://r.789695.n4.nabble.com/Pasting-function-arguments-and-strings-tp2993905p2993976.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
Re: [R] Pasting function arguments and strings
From what I read, you want something like this: myfunction-function(dataset,arg1,arg2) { func = match.fun(arg2) argument = dataset[, match(paste(arg1,_test, sep=), names(dataset))] result=func(argument) return(result) } On Wed, Oct 13, 2010 at 9:28 AM, Manta mantin...@libero.it wrote: Thaks for your quick reply Bert, although I doubt it works. The reason is that the names of the objects of the dataset, all end with the sufix '_test' and therefore I need to attach/paste/glue this suffix to the 'arg2' of the function. Any other idea? -- View this message in context: http://r.789695.n4.nabble.com/Pasting-function-arguments-and-strings-tp2993905p2993976.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.
Re: [R] Pasting function arguments and strings
Is this the sort of thing you are looking for? f - function(testName, dataset, ...) { + testFunc - match.fun(paste(testName, _test, sep=)) + testFunc(dataset, ...) + } m_test - function(x) the m test p_test - function(x) the p test f(m, 17) [1] the m test Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Manta Sent: Wednesday, October 13, 2010 9:29 AM To: r-help@r-project.org Subject: Re: [R] Pasting function arguments and strings Thaks for your quick reply Bert, although I doubt it works. The reason is that the names of the objects of the dataset, all end with the sufix '_test' and therefore I need to attach/paste/glue this suffix to the 'arg2' of the function. Any other idea? -- View this message in context: http://r.789695.n4.nabble.com/Pasting-function-arguments-and-s trings-tp2993905p2993976.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.