s-news
[Top] [All Lists]

Fwd: lapply question

To: yang_eric9@yahoo.com
Subject: Fwd: lapply question
From: "Richard M. Heiberger" <rmh@temple.edu>
Date: Tue, 30 Aug 2005 12:28:54 -0400
Cc: s-news@lists.biostat.wustl.edu
As written, count is an argument to the two inline functions
inside the lapply statements.  Therfore it is local to each of those
functions and the updates are lost when the function exits.  To get the
behavior you want, you will need to remove count from the argument
list and instead assign "y" in frame 0.

> tmp <- function(x) {assign("y", frame=0, x+3)}
> y
Problem: Object "y" not found 
Use traceback() to see the call stack
> tmp(4)
NULL
> y
[1] 7
> 
> objects(1, p="y")
character(0)
> objects(0, p="y")
[1] "y"
>

---- Original message ----
>Date: Tue, 30 Aug 2005 09:11:19 -0700 (PDT)
>From: Eric yang <yang_eric9@yahoo.com>  
>Subject: [S] lapply question  
>To: s-news@lists.biostat.wustl.edu
>
>    
>   Dear members,
>    
>   If I have the following lapply loops
>    
>
>   lapply(10:20, function(i) {
>
>      count <- 1
>
>      lapply(1:20, function(j, i, count) {
>
>          lapply(1:5, function(k,j,i,count){
>
>            count <- count +1
>
>             rnorm(count, j, i)
>
>          }, j, i, count)
>
>       }, i, count)
>
>   })
>
>   How do I update the parameter "count" within the two inner-lapply loops. At 
> present, count
>   is always 2?
>
>   Thanks in advance for any help.
>
>   Eric
>
>   __________________________________________________
>   Do You Yahoo!?
>   Tired of spam? Yahoo! Mail has the best spam protection around
>   http://mail.yahoo.com

<Prev in Thread] Current Thread [Next in Thread>
  • lapply question, Eric yang
    • Fwd: lapply question, Richard M. Heiberger <=