[R] Acumulate assign on lapply list

Charles C. Berry cberry at tajo.ucsd.edu
Thu Dec 3 18:04:57 CET 2009


On Thu, 3 Dec 2009, KENNETH R CABRERA wrote:

> Hi R users:
>
> f1<-function(l1,idx){
>   f2<-function(i,l1)   l1[[i+1]]<<-c(l1[[i]],l1[[i+1]][-1])
>   lapply(idx,f2,l1)
>   return(l1)
> }
>
> l<-list(c(1,2,3),c(4,5),c(6,7,8))
>
> l2<-f1(l,1:(length(l)-1))
>
> l2
>
> I got:
>
> [[1]]
> [1] 1 2 3
>
> [[2]]
> [1] 1 2 3 5
>
> [[3]]
> [1] 4 5 7 8
>
> But what I want in the last position of l2 is the acummualte of the
> assignations...
>
> I would like to have:
>
> [[1]]
> [1] 1 2 3
>
> [[2]]
> [1] 1 2 3 5
>
> [[3]]
> [1] 1 2 3 5 7 8
>
> How can I do that?


 	Reduce( function(x,y) c( x, y[-1] ) , l , accumulate=TRUE)

HTH,

Chuck

>
> Thank you for your help.
>
> Kenneth
>
>
>
> 	[[alternative HTML version deleted]]
>
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list