s-news
[Top] [All Lists]

assign Vs <<-

To: <s-news@lists.biostat.wustl.edu>
Subject: assign Vs <<-
From: "Steve Su" <s.su@qut.edu.au>
Date: Thu, 28 Nov 2002 18:05:38 +1000
Importance: Normal
Dear All,

Many thanks for Nick and Dimitris comments.

To avoid using assign inside a function when you want to use lapply
within the function as detailed in the mail below, you can use:

Some.func<-function(object){
 lapply(1:10, function(i,obj) obj[[i]], obj = object) 
}

Steve. 



-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Steve Su
Sent: Thursday, 28 November 2002 3:24 PM
To: s-news@lists.biostat.wustl.edu
Subject: [S] assign Vs <<-

Dear All,

I have discovered that it is often the case that you will need to use
assign(?object?,object,immediate=T,frame=1) inside a function if you
want to use the lapply command in the following form: 

Some.func<-function(object){

assign(?object?,object,immediate=T,frame=1)
lapply(1:10, function(i) object[[i]]) 
}

Alternatively I can get function to work by using <<-

Some.func<-function(object){

object<<-object
lapply(1:10, function(i) object[[i]]) 
}

The downside of using <<- is that it creates the object as a permanent
object but it does seem to help my simulation runs a lot faster. I am
thinking of using <<- and then use rm(object) at the end to remove the
object, so I can make my simulation run faster without creating a huge
list of objects in my directory. I am wondering if there is an
alternative way of doing this?

All comments are welcome!







************************************************************************
**************

 Steve Su (s.su@qut.edu.au)   
 PhD student. 

 School of Accountancy
 School of Mathematical Sciences
 Queensland University of Technology 

 Postal Address: Steve Su, School of Accountancy, QUT, PO Box 2434,
Brisbane,
 Queensland, Australia, 4000.  


 Phone:  +61 7 3864 2017
 Fax:   ; +61 7 3864 1812     
 Mobile: 0421  840  586 
     .                              
   _--_|\                 
  /      QUT                                  
 
\_.--._/                                                                
          
        v                                              
                                                                     
************************************************************************
**************     

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news


<Prev in Thread] Current Thread [Next in Thread>