Dear users, I'm trying to learn how to use the "...". I have written a function (simplified here) that uses doBy::summaryBy(): # 'dat' is a data.frame from which the aggregation is computed # 'vec_cat' is a integer vector defining which columns of the data.frame should be use on the right side of the formula # 'stat_fun' is the function that will be run to aggregate stat.group <- function(dat, vec_cat, stat_fun){ require(doBy) df <- summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))), data=dat, FUN=stat_fun) return(df) } Example, works fine: my_data <- structure(list(cat = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), varnum = c(-0.754816565434373, -1.94101630973709, -0.102461836059522, -0.519952759645808, -1.73772800855664, -1.13939178585609, 0.522356715260142, -0.701428514907824, 1.45197576541159, 0.0844567413828095)), .Names = c("cat", "varnum"), row.names = c(NA, -10L), class = "data.frame") stat.group(dat=my_data, vec_cat=1, stat_fun=mean) Now summaryBy() has an "..." argument and I would like to use it. For example, I would like to be able to add the trim argument to my call like this: stat.group(dat=my_data, vec_cat=1, stat_fun=mean, trim=0.2) I know I can do it using this "..." but I have no idea how to do it. I've tried to search for it, but a search with "..." doesn't yield interesting results! Thank you in advance for your help! Ivan -- Ivan CALANDRA Universit? de Bourgogne UMR CNRS/uB 6282 Biog?osciences 6 Boulevard Gabriel 21000 Dijon, FRANCE +33(0)3.80.39.63.06 ivan.calandra at u-bourgogne.fr http://biogeosciences.u-bourgogne.fr/calandra
On Thu, Jan 17, 2013 at 2:36 PM, Ivan Calandra <ivan.calandra at u-bourgogne.fr> wrote:> Dear users, > > I'm trying to learn how to use the "...". > > I have written a function (simplified here) that uses doBy::summaryBy(): > # 'dat' is a data.frame from which the aggregation is computed > # 'vec_cat' is a integer vector defining which columns of the data.frame > should be use on the right side of the formula > # 'stat_fun' is the function that will be run to aggregate > stat.group <- function(dat, vec_cat, stat_fun){ > require(doBy) > df <- > summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))), > data=dat, FUN=stat_fun) > return(df) > } > [SNIP EXAMPLE -- THANK YOU FOR IT] > > Now summaryBy() has an "..." argument and I would like to use it. > For example, I would like to be able to add the trim argument to my call > like this: > stat.group(dat=my_data, vec_cat=1, stat_fun=mean, trim=0.2) > >Thanks for the great working examples! It's actually not too hard here -- just pass "..." down as if it were an argument and let summaryBy() do the hard work of actually handling the dots: stat.group <- function(dat, vec_cat, stat_fun, ...){ require(doBy) df <- summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))), data=dat, FUN=stat_fun, ...) return(df) } Also, note that as a matter of style, you can actually clean this up a little bit: R follows the trend of many functional languages in automatically returning the value of the last expression evaluated: stat.group <- function(dat, vec_cat, stat_fun, ...){ require(doBy) summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))), data=dat, FUN=stat_fun, ...) } Cheers, Michael
On 17-01-2013, at 15:36, Ivan Calandra <ivan.calandra at u-bourgogne.fr> wrote:> Dear users, > > I'm trying to learn how to use the "...". > > I have written a function (simplified here) that uses doBy::summaryBy(): > # 'dat' is a data.frame from which the aggregation is computed > # 'vec_cat' is a integer vector defining which columns of the data.frame should be use on the right side of the formula > # 'stat_fun' is the function that will be run to aggregate > stat.group <- function(dat, vec_cat, stat_fun){ > require(doBy) > df <- summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))), data=dat, FUN=stat_fun) > return(df) > } > > Example, works fine: > my_data <- structure(list(cat = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, > 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), varnum = c(-0.754816565434373, > -1.94101630973709, -0.102461836059522, -0.519952759645808, -1.73772800855664, > -1.13939178585609, 0.522356715260142, -0.701428514907824, 1.45197576541159, > 0.0844567413828095)), .Names = c("cat", "varnum"), row.names = c(NA, > -10L), class = "data.frame") > stat.group(dat=my_data, vec_cat=1, stat_fun=mean) > > > Now summaryBy() has an "..." argument and I would like to use it. > For example, I would like to be able to add the trim argument to my call like this: > stat.group(dat=my_data, vec_cat=1, stat_fun=mean, trim=0.2) > > > I know I can do it using this "..." but I have no idea how to do it. I've tried to search for it, but a search with "..." doesn't yield interesting results! >This seems to work: stat.group <- function(dat, vec_cat, stat_fun,...){ require(doBy) df <- summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))), data=dat, FUN=stat_fun,...) return(df) } and stat.group(dat=my_data, vec_cat=1, stat_fun=mean, trim=0.2) as the example for sumfun in the help for summaryBy shows. Berend
There is now a blog post that attempts to answer the question in the subject line: http://www.burns-stat.com/the-three-dots-construct-in-r/ Pat On 17/01/2013 14:36, Ivan Calandra wrote:> Dear users, > > I'm trying to learn how to use the "...". > > I have written a function (simplified here) that uses doBy::summaryBy(): > # 'dat' is a data.frame from which the aggregation is computed > # 'vec_cat' is a integer vector defining which columns of the data.frame > should be use on the right side of the formula > # 'stat_fun' is the function that will be run to aggregate > stat.group <- function(dat, vec_cat, stat_fun){ > require(doBy) > df <- > summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))), > data=dat, FUN=stat_fun) > return(df) > } > > Example, works fine: > my_data <- structure(list(cat = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, > 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), varnum > c(-0.754816565434373, > -1.94101630973709, -0.102461836059522, -0.519952759645808, > -1.73772800855664, > -1.13939178585609, 0.522356715260142, -0.701428514907824, 1.45197576541159, > 0.0844567413828095)), .Names = c("cat", "varnum"), row.names = c(NA, > -10L), class = "data.frame") > stat.group(dat=my_data, vec_cat=1, stat_fun=mean) > > > Now summaryBy() has an "..." argument and I would like to use it. > For example, I would like to be able to add the trim argument to my call > like this: > stat.group(dat=my_data, vec_cat=1, stat_fun=mean, trim=0.2) > > > I know I can do it using this "..." but I have no idea how to do it. > I've tried to search for it, but a search with "..." doesn't yield > interesting results! > > > Thank you in advance for your help! > Ivan >-- Patrick Burns pburns at pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming')