[R] Trouble passing list or non-list to function using ...

Gabor Grothendieck ggrothendieck at gmail.com
Sat May 27 03:04:17 CEST 2006


Its not clear from your post which of the tests does not give you
the desired output and what the desired output is in that case.

Just guessing but maybe you meant to use testlist[[i]] instead of testlist[i] in
the loop?


On 5/26/06, Jason Barnhart <jasoncbarnhart at msn.com> wrote:
> Hello,
>
> Simply put, I'm trying to call a function "testme" with value "age=NA".   I wish to use "dotlist<-list(...)"  inside the function and have dotlist become:
> $age
> [1] NA
>
> I'm modifying existing code and need to minimize changing that code so it's easiest to conform
> how I call the existing function.
>
> My sample code fragment, results, and R.version information are listed below.   I've been searching
> for prior questions regarding this phenomena, but haven't quite figured out how to do this.
>
> Thank you in advance,
> -jason
>
>
>
> ##BEGIN TEST CODE ###########################################################
> testme<-function(...){
>
> dotlist<-list(...)
>
>    dotlist
>
> }
>
> nm<-"age"
>
> testme(age=NA)
>
> testme(nm=NA)
>
> testme(age=NA,age2=NA)
>
> testlist<-list("age","age2")
>
> for (i in 1:length(testlist)){
>
>    print(testme(testlist[i]))
>
> }
>
> ##END TEST CODE #############################################################
>
> ##BEGIN RESULTS #############################################################
> > testme<-function(...){
> +
> + dotlist<-list(...)
> +
> +     dotlist
> +
> + }
> >
> > nm<-"age"
> >
> > testme(age=NA)   *****This is what I'm really after****
> $age
> [1] NA
>
> >
> > testme(nm=NA)
> $nm
> [1] NA
>
> > #trying w/ 2 vars
> > testme(age=NA,age2=NA)
> $age
> [1] NA
>
> $age2
> [1] NA
>
> >
> > testlist<-list("age","age2")
> >
> > for (i in 1:length(testlist)){
> +
> +     print(testme(testlist[i]))
> +
> + }
> [[1]]
> [[1]][[1]]
> [1] "age"
>
>
> [[1]]
> [[1]][[1]]
> [1] "age2"
>
> ##END RESULTS ###############################################################
>
> platform       i386-pc-mingw32
> arch           i386
> os             mingw32
> system         i386, mingw32
> status
> major          2
> minor          3.0
> year           2006
> month          04
> day            24
> svn rev        37909
> language       R
> version.string Version 2.3.0 (2006-04-24)
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
>



More information about the R-help mailing list