Thanks Baptiste



On Fri, Sep 4, 2009 at 5:13 PM, baptiste auguie <
baptiste.auguie@googlemail.com> wrote:

> Hi,
>
> I think you've got a problem with environments,
>
> testA<-function(input=1)
>
> {
>
>      dat <- data.frame(A=seq(input,5), B=seq(6,10))
>      vec.names<- c("a", "b")
>      env <- new.env()
>
>     for(i in 1:ncol(dat))
>      {
>            tab<- dat[,i]-1
>            assign(vec.names[i], tab, env=env)
>      }
>      do.call("rbind", lapply(vec.names, get, env=env))
> }
>
> testA()
>
> But more generally, I doubt your construct using assign and get is the most
> natural way to reach your goal in R.
>
> HTH,
>
> baptiste
>
>
> 2009/9/4 jonas garcia <garcia.jonas80@googlemail.com>
>
>>  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<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> _____________________________
>
> Baptiste Auguié
>
> School of Physics
> University of Exeter
> Stocker Road,
> Exeter, Devon,
> EX4 4QL, UK
>
> http://newton.ex.ac.uk/research/emag
> ______________________________
>
>

	[[alternative HTML version deleted]]


