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@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.

Reply via email to