Eric Lecoutre
2004-Nov-09 08:12 UTC
[R] do.call invoking a function with multiple arguments
Hi users, I am not sure to understand the help page about do.call. -- do.call(what, args) args: a _list_ of arguments to the function call. The 'names' attribute of 'args' gives the argument names. -- If we take the following sample data: > (tab=as.data.frame(table(factor(1:10)))) Var1 Freq 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 6 1 7 7 1 8 8 1 9 9 1 10 10 1 I can call "paste" on it: > do.call("paste",tab) [1] "1 1" "2 1" "3 1" "4 1" "5 1" "6 1" "7 1" "8 1" "9 1" "10 1" Now if I want to use ":" as the separator, I guess I should write > do.call("paste",list(tab,sep=":")) [1] "c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)" "c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)" which does not return me what I want. Of course, I know there are plenty other ways to achieve my goal but this is not the first case I encounter I cant' use "do.call" though I think it should be possible. Could someone bring me an example of such a use? basically, my needs are: do.call("foo",args) args containing primary and extra-arguments to pass to "foo". Best wishes, Eric Eric Lecoutre UCL / Institut de Statistique Voie du Roman Pays, 20 1348 Louvain-la-Neuve Belgium tel: (+32)(0)10473050 lecoutre at stat.ucl.ac.be http://www.stat.ucl.ac.be/ISpersonnel/lecoutre If the statistics are boring, then you've got the wrong numbers. -Edward Tufte
Prof Brian Ripley
2004-Nov-09 08:46 UTC
[R] do.call invoking a function with multiple arguments
On Tue, 9 Nov 2004, Eric Lecoutre wrote:> I am not sure to understand the help page about do.call.I think you do, just not how to construct a list.> -- > do.call(what, args) > args: a _list_ of arguments to the function call. The 'names' > attribute of 'args' gives the argument names. > -- > > If we take the following sample data: > > > > (tab=as.data.frame(table(factor(1:10)))) > Var1 Freq > 1 1 1 > 2 2 1 > 3 3 1 > 4 4 1 > 5 5 1 > 6 6 1 > 7 7 1 > 8 8 1 > 9 9 1 > 10 10 1 > > I can call "paste" on it: > > > do.call("paste",tab) > [1] "1 1" "2 1" "3 1" "4 1" "5 1" "6 1" "7 1" "8 1" "9 1" "10 1" > > Now if I want to use ":" as the separator, I guess I should write > > > do.call("paste",list(tab,sep=":")) > [1] "c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)" "c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)"But tab is already a list, so you want to concatenate to it. do.call("paste", c(tab,sep=":")) Take a look at str(list(tab,sep=":")) which shows it is a list of 2 elements, the first of which is a data frame (which is a list). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Dimitris Rizopoulos
2004-Nov-09 08:59 UTC
[R] do.call invoking a function with multiple arguments
Hi Eric, you could try it this way:> tab <- as.data.frame(table(factor(1:10))) > do.call("paste", c(tab, list(sep=":")))[1] "1:1" "2:1" "3:1" "4:1" "5:1" "6:1" "7:1" "8:1" "9:1" "10:1" since `tab' is already a list Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Eric Lecoutre" <lecoutre at stat.ucl.ac.be> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, November 09, 2004 9:12 AM Subject: [R] do.call invoking a function with multiple arguments> > Hi users, > > I am not sure to understand the help page about do.call. > > -- > do.call(what, args) > args: a _list_ of arguments to the function call. The 'names' > attribute of 'args' gives the argument names. > -- > > If we take the following sample data: > > > > (tab=as.data.frame(table(factor(1:10)))) > Var1 Freq > 1 1 1 > 2 2 1 > 3 3 1 > 4 4 1 > 5 5 1 > 6 6 1 > 7 7 1 > 8 8 1 > 9 9 1 > 10 10 1 > > I can call "paste" on it: > > > do.call("paste",tab) > [1] "1 1" "2 1" "3 1" "4 1" "5 1" "6 1" "7 1" "8 1" "9 1" > "10 1" > > Now if I want to use ":" as the separator, I guess I should write > > > do.call("paste",list(tab,sep=":")) > [1] "c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)" "c(1, 1, 1, 1, 1, 1, 1, 1, 1, > 1)" > > which does not return me what I want. > Of course, I know there are plenty other ways to achieve my goal but > this is not the first case I encounter I cant' use "do.call" though > I think it should be possible. > > Could someone bring me an example of such a use? > basically, my needs are: > > do.call("foo",args) args containing primary and extra-arguments to > pass to "foo". > > Best wishes, > > Eric > > > Eric Lecoutre > UCL / Institut de Statistique > Voie du Roman Pays, 20 > 1348 Louvain-la-Neuve > Belgium > > tel: (+32)(0)10473050 > lecoutre at stat.ucl.ac.be > http://www.stat.ucl.ac.be/ISpersonnel/lecoutre > > If the statistics are boring, then you've got the wrong > numbers. -Edward Tufte > > ______________________________________________ > 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 >