Skip to content

do.call invoking a function with multiple arguments

3 messages · Eric Lecoutre, Brian Ripley, Dimitris Rizopoulos

#
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
#
On Tue, 9 Nov 2004, Eric Lecoutre wrote:

            
I think you do, just not how to construct a list.
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).
#
Hi Eric,

you could try it this way:
[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