[R] how to update a value in a list with lapply

William Dunlap wdunlap at tibco.com
Mon May 11 00:00:21 CEST 2015


You can do the timing yourself on a dataset which you feel is typical of
your usage.
E.g., define a function the implements each algorithm
  > f1 <- function(foo) lapply(foo, function(x) { if (x[1] == 1) x[2] <- 0
; x })
  > f2 <- function(foo) { for(i in seq_along(foo)) if (foo[[i]][1] == 1)
foo[[i]][2] <- 0 ; foo }
and compare the times (and return values) on various datasets.
  > foo1 <- rep(list(c(1,2,1:100)), length=1e5) # every element needs
changing
  > system.time(v1 <- f1(foo1))
     user  system elapsed
     0.28    0.01    0.29
  > system.time(v2 <- f2(foo1))
     user  system elapsed
     0.26    0.03    0.30
  > identical(v1, v2)
  [1] TRUE
  > foo2 <- rep(list(c(0,2,1:100)), length=1e5) # no element needs changing
  > system.time(v1 <- f1(foo2))
     user  system elapsed
     0.09    0.00    0.09
  > system.time(v2 <- f2(foo2))
     user  system elapsed
     0.07    0.00    0.07
  > identical(v1, v2)
  [1] TRUE




Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sun, May 10, 2015 at 6:11 AM, ce <zadig_1 at excite.com> wrote:

>
> yes indeed :
>
>  foo <- lapply(foo, function(x) if(x[1] == 1 ) {x[2] <- 0; x }else{x} )
>
> would work. But if the list is too long, would it be time consuming
> rather than just updating elements that meet the if condition?
>
> thx
> ce
>
>
> -----Original Message-----
> From: "David Winsemius" [dwinsemius at comcast.net]
> Date: 05/09/2015 08:00 PM
> To: "ce" <zadig_1 at excite.com>
> CC: r-help at r-project.org
> Subject: Re: [R] how to update a value in a list with lapply
>
>
> On May 9, 2015, at 4:35 PM, ce wrote:
>
> > Dear All,
> >
> > I have a list, using lapply I find some elements of the list, and then I
> want to change the values I find. but it doesn't work:
> >
> > foo<-list(A = c(1,3), B =c(1, 2), C = c(3, 1))
> > lapply(foo, function(x) if(x[1] == 1 ) x )
> > $A
> > [1] 1 3
> >
> > $B
> > [1] 1 2
> >
> > $C
> > NULL
> >
> > lapply(foo, function(x) if(x[1] == 1 ) x[2] <- 0 )
> > $A
> > [1] 0
> >
> > $B
> > [1] 0
> >
> > $C
> > NULL
> >
> >> lapply(foo, function(x) if(x[1] == 1 ) x )
> > $A
> > [1] 1 3
> >
> > $B
> > [1] 1 2
> >
> > $C
> > NULL
> >
> >
> > how to do it correctly ?
>
> I find it useful to think of the `if` function as `if(cond){cons}else{alt}`
>
> lapply(foo, function(x)  if(x[1] == 1 ) {x[2] <- 0; x }else{x} )
> #-----
> $A
> [1] 1 0
>
> $B
> [1] 1 0
>
> $C
> [1] 3 1
>
>
> You were not supply an alternative which was the cause of the NULL (and
> you were not returning a value which meant that the value returned was the
> value on the RHS of the assignment).
>
> --
>
> David Winsemius
> Alameda, CA, USA
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list