--- Begin Message ---
Thank you for your help.

It is just what I want!!


El mié, 02-12-2009 a las 12:08 -0800, William Dunlap escribió:
> > -----Original Message-----
> > From: Kenneth Roy Cabrera Torres [mailto:krcab...@une.net.co] 
> > Sent: Wednesday, December 02, 2009 12:01 PM
> > To: William Dunlap
> > Subject: RE: [R] lapply assign to list
> > 
> > Thank you William Dunlap:
> > 
> > What can I do if I want to apply the
> > same function to z1, z2, etc...
> > 
> > Should I define f1, f2, f3... for each
> > z1, z2, and z3..
> > 
> > How can I generalize?
> 
> Make a function with an argument zn and
> do all calculations inside of that function.
> You can leave out the local() then.
> E.g.,
> > g <- function(zn) {
> +    f <- function(i){ zn[[i]] <<- log(i); sqrt(i) }
> +    value <- lapply(1:3, f)
> +    list(value=value, zn=zn)
> + }
> > g(list("A", 11:13, 1+2i))
> $value
> $value[[1]]
> [1] 1
> 
> $value[[2]]
> [1] 1.414214
> 
> $value[[3]]
> [1] 1.732051
> 
> 
> $zn
> $zn[[1]]
> [1] 0
> 
> $zn[[2]]
> [1] 0.6931472
> 
> $zn[[3]]
> [1] 1.098612
> > 
> > Again, thank you very much for your help.
> > 
> > Kenneth
> > 
> > 
> > 
> > El mié, 02-12-2009 a las 11:42 -0800, William Dunlap escribió:
> > > Functions cannot alter their arguments
> > > so modifying f1's l1 argument would not
> > > modify anything outside of f1.
> > > 
> > > You could put l1 into f1's environment
> > > so it could be modified with <<-.  E.g.,
> > > 
> > > > l1<-z1
> > > > f1 <- local({ l1 <- z1;
> > > +               function(i){
> > > +                  print(l1[[i]])
> > > +                  l1[[i+1]] <<- i
> > > +                  i
> > > +               }
> > > +             })
> > > > lapply(1:3,f1)
> > > [1] 1
> > > [1] 1
> > > [1] 2
> > > [[1]]
> > > [1] 1
> > > 
> > > [[2]]
> > > [1] 2
> > > 
> > > [[3]]
> > > [1] 3
> > > 
> > > Note that z1 is not altered.  To see the final
> > > value of f1's l1 do, e.g.,
> > > > get("l1", env=environment(f1))
> > > $x
> > > [1] 1
> > > 
> > > [[2]]
> > > [1] 1
> > > 
> > > $a
> > > [1] 2
> > > 
> > > [[4]]
> > > [1] 3
> > > 
> > > Bill Dunlap
> > > Spotfire, TIBCO Software
> > > wdunlap tibco.com  
> > > 
> > > > -----Original Message-----
> > > > From: r-help-boun...@r-project.org 
> > > > [mailto:r-help-boun...@r-project.org] On Behalf Of Kenneth 
> > > > Roy Cabrera Torres
> > > > Sent: Wednesday, December 02, 2009 11:18 AM
> > > > To: r-help@r-project.org
> > > > Subject: [R] lapply assign to list
> > > > 
> > > > Hi R users:
> > > > 
> > > > I got an error that "l1" is not found.
> > > > 
> > > > This is a sample code:
> > > > 
> > > > f1<-function(i,l1){
> > > >   print(l1[[i]])
> > > >   l1[[i+1]]<<-i
> > > >   return(i)
> > > > }
> > > > 
> > > > z1<-list(x=100,NULL,a=c(1,1))
> > > > lapply(1:3,f1,l1=z1)
> > > > 
> > > > My final goal is to update the value of
> > > > each list component to use it in the following
> > > > step.
> > > > 
> > > > I see that print function founds l1[[1]],
> > > > but the assign instruction not.
> > > > 
> > > > Thank you for your help.
> > > > 
> > > > Kenneth
> > > > 
> > > > PD: R.Version()$version.string
> > > > [1] "R version 2.10.0 Patched (2009-11-24 r50556)"
> > > > on Ubuntu linux.
> > > > 
> > > > ______________________________________________
> > > > 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.
> > > > 
> > 
> > 

--- End Message ---
______________________________________________
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