[R] help with functions

jim holtman jholtman at gmail.com
Sat Sep 5 21:56:04 CEST 2009


Try this variation; might have something to do with scope:

testA<-function(input)
{
     dat<- data.frame(A=seq(input,5), B=seq(6,10))
     vec.names<- c("a", "b")
     for(i in 1:ncol(dat))
     {
           tab<- dat[,i]-1
           assign(vec.names[i], tab)
     }
     do.call("rbind", lapply(vec.names, function(x) get(x)))
}

Just changed the way 'get' was done in the lapply

On Fri, Sep 4, 2009 at 10:00 AM, jonas
garcia<garcia.jonas80 at googlemail.com> wrote:
> Hi all,
>
>
>
> I have got 2 function (see bellow) which are simplifications of what I need
> to do. These functions are precisely the same, except for the last line.
>
>
>
> My question is, why doesn't function testA work in the same way as function
> testB.
>
> Both functions produce two objects, "a" and "b" that must merged with rbind.
> The difference is that in testA, I specify the name of the objects while in
> testA I am stating which objects I want to bind from a character vector.
>
>
>
> What's more, if I just run the code without a function (example given below
> as well), they both work...
>
> Why is this?
>
>
>
> Thanks in advance
>
>
>
> Jonas
>
>
>
>
>
> testA<-function(input)
>
> {
>
>      dat<- data.frame(A=seq(input,5), B=seq(6,10))
>
>      vec.names<- c("a", "b")
>
>      for(i in 1:ncol(dat))
>
>      {
>
>            tab<- dat[,i]-1
>
>            assign(vec.names[i], tab)
>
>      }
>
>
>
>      do.call("rbind", lapply(vec.names, get))
>
> }
>
>
>
>
>
>
>
> testB<-function(input)
>
> {
>
>      dat<- data.frame(A=seq(input,5), B=seq(6,10))
>
>      vec.names<- c("a", "b")
>
>      for(i in 1:ncol(dat))
>
>      {
>
>            tab<- dat[,i]-1
>
>            assign(vec.names[i], tab)
>
>      }
>
>
>
>      rbind(a,b)
>
> }
>
>
>
>
>
>
>
> testA(1)
>
> Error in FUN(c("a", "b")[[1L]], ...) : object 'a' not found
>
>
>
> testB(1)
>
>  [,1] [,2] [,3] [,4] [,5]
>
> a    0    1    2    3    4
>
> b    5    6    7    8    9
>
>
>
>
>
>
>
> ###################################################################################
>
>
>
>
>
>
>
>      dat<- data.frame(A=seq(1,5), B=seq(6,10))
>
>      vec.names<- c("a", "b")
>
>      for(i in 1:ncol(dat))
>
>      {
>
>            tab<- dat[,i]-1
>
>            assign(vec.names[i], tab)
>
>      }
>
>
>
>      do.call("rbind", lapply(vec.names, get))
>
>     [,1] [,2] [,3] [,4] [,5]
>
> [1,]    0    1    2    3    4
>
> [2,]    5    6    7    8    9
>
>
>
>
>
>
>
>
>
> dat<- data.frame(A=seq(1,5), B=seq(6,10))
>
>      vec.names<- c("a", "b")
>
>      for(i in 1:ncol(dat))
>
>      {
>
>            tab<- dat[,i]-1
>
>            assign(vec.names[i], tab)
>
>      }
>
>
>
>      rbind(a,b)
>
>  [,1] [,2] [,3] [,4] [,5]
>
> a    0    1    2    3    4
>
> b    5    6    7    8    9
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list