[R] Need to append vector to all levels of nested list WITHOUT a loop

Rui Barradas ruipbarradas at sapo.pt
Tue Jun 19 08:18:23 CEST 2012


Hello,

Try

(b <- list(list(m=4, v=5), list(m=6, v=7)))
(m <- matrix(1:6, 2))

b2 <- lapply(seq_along(b), function(i){
	b[[i]]$line <- m[i, ]; b[[i]]})
b2


Also, next time, post a data example, using dput().

Hope this helps,

Rui Barradas

Em 18-06-2012 14:54, Johannes Reichl escreveu:
> Hi everyone,
>
> I have a list betaMoments with 2 levels, e.g.
>
>> beta1 = list(
> +		 m = 4,
> +		 v = 5
> +		 )
>> beta2 = list(
> +		 m = 6,
> +		 v = 7
> +		 )
>>
>> betaMoments = list()
>> betaMoments[[1]] =  beta1
>> betaMoments[[2]] =  beta2
>
> and I have a matrix Names which has the same number of lines as the
> list has first level elements (here 2; beta1 and beta2). Now I need to
> make every line of Names (e.g. Names[1,] = c(2,3,4)) to become an
> element of the corresponding list element. For example, calling
> betaMoments[[1]] shall return
>
>> betaMoments[[1]]
> $m
> [1] 4
>
> $v
> [1] 5
>
> $line
> [1] c(2,3,4) ... thats Names[1,]
>
> I can not do it with a loop since I need to execute this list
> construction way too often. So I need a speedy solution.
>
> Would be great if someone could help me, this really keeps me away from
> resolving the true issues.
>
> Regards, Johannes
>
>
>
>
> ********************************************************************
> Dr. Johannes Reichl
> Project Manager
> Energy Institute at the Johannes Kepler University Linz
> Altenberger Straße 69
> A-4040 Linz
> Tel.: +43-732-2468-5652
> Fax: +43-732-2468-5651
> Email: reichl at energieinstitut-linz.at
> Web: www.energieinstitut-linz.at
>           www.energyefficiency.at
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list