On 07/02/2016 6:12 PM, Robert Sherry wrote:

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(...)

This is wrong. The ... object is weird; it's not something that can be coerced to a list. However, you can pass it as list(...) and it will give you what you were expecting.

The theory is that it will expand to multiple arguments to the list() function, which constructs a list containing them. as.list() doesn't want a bunch of arguments, it will just ignore most of them.

Duncan Murdoch

      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

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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.

Reply via email to