[R] Variable Argument Function

Robert Sherry rsherry8 at comcast.net
Mon Feb 8 00:12:32 CET 2016


I would like to write a function in R that would take a variable number 
of integers as parameters. I do not have a pressing reason to do this, I 
am just trying to learn R. I thought a good first step would be to print 
out the arguments. So I wrote the following function:

f1 = function (...)
{
     list1 = as.list(...)
     for( i in 1:length(list1) )
         cat( "i is ", list1[[i]], "\n" )
     return (0)
}

I ran it as:
     f1(2,4,10,12)
and I get:
     i is  2
     [1] 0
I was hoping for
     i is  2
     i is  4
     i is  10
     i is  12

I am hoping somebody can tell me what I am doing wrong. Is using a list 
a bad idea?

Thanks
Bob



More information about the R-help mailing list