[R-sig-teaching] count the number of argument of a function
Randall Pruim
rpruim at calvin.edu
Tue Oct 20 23:15:14 CEST 2015
A couple more questions for you:
* what type of object do you want created? (A matrix? data frame? something else?)
* is there a reason to have 3 named arguments and then … if you are treating them all the same in the end anyway? Seems like that just makes your function less flexible and harder to code. But I don’t know your use case.
As the previously sent example shows, you can easily apply cbind to all of the items in … (without needing to know how many there were). I’m now guessing that your attempt to calculate m was not needed. You can always recover it from the number of columns in the object produced by cbind() if you need it later.
Good luck with what I hope is the more interesting part of your programming project that comes after this.
—rjp
> On Oct 20, 2015, at 4:17 PM, Steven Stoline <sstoline at gmail.com> wrote:
>
> Dear Michael:
>
> Thank you very much for your quick reply.
>
> Just one more thing. Assume we have these three data sets (vectors), but
> could be 4, 5 or more data sets.
>
> x1<-c(1,2,3,4,5)
> x2<-c(11,22,33,44,55)
> x3<-c(111,222,333,444,555)
>
>
> Inside the same fumction I do need to use the cbind() function to cbind
> these m (m could be 2, 3, 4, or any numbers) data sets. This what I trired,
> but it did not work.
>
> f <- function(x1,x2,x3){
> m<-length(formals(sys.function()))
>
> print(m)
>
> data<-cbind(x1,x2,...,xm) #### I need
> some help with this part
>
> print(data)
>
> }
>
> f(x1,x2,x3)
>
>
>
> with thanks
> steve
>
>
>
> On Tue, Oct 20, 2015 at 3:51 PM, Michael Weylandt <
> michael.weylandt at gmail.com> wrote:
>
>> On Tue, Oct 20, 2015 at 2:45 PM, Steven Stoline <sstoline at gmail.com>
>> wrote:
>>
>>> Dear All:
>>>
>>> I am wondering whether there is a way to read and assign the number of
>>> arguments of a function inside this function.
>>>
>>>
>>> ### For example
>>>
>>> fun<-function(x1,x2,x3,...){
>>>
>>> m<- number of arguments
>>>
>>> }
>>>
>>> ### e.g.
>>>
>>> fun<-function(x1,x2,x3){
>>>
>>> m<- number of arguments
>>>
>>> ### m =3 in this case
>>>
>>> }
>>>
>>> ### e.g.
>>>
>>> fun<-function(x1,x2,x3,y1,z1,z2){
>>>
>>> m<- number of arguments
>>>
>>> ### m =6 in this case
>>>
>>> }
>>>
>>
>>
>> It's not entirely kosher, but
>>
>> length(formals(sys.function()))
>>
>> will work.
>>
>> f <- function(a, b,c, d, e, f){
>> length(formals(sys.function()))
>> }
>>
>> f() ## 6
>>
>
>
>
> --
> Steven M. Stoline
> 1123 Forest Avenue
> Portland, ME 04112
> sstoline at gmail.com
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-teaching at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
More information about the R-sig-teaching
mailing list