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

Rui Barradas ruipbarradas at sapo.pt
Tue Jun 19 10:46:01 CEST 2012


Hello, again.

I really don't know of any advanced tutorial specifically on lists but 
I'm posting a link below. I believe the best way is practice. Some 
things to remember:

1. Many times, almost always, it's better to try the function to apply 
in one or two examples. If it works in the small, it might work with the 
entire dataset.

2. Changes made inside functions do not become effective outside them. 
Assignments are not global, R passes arguments by value, not by 
reference. So sometimes, not always, there be a return value. See Circle 
6 of the R Inferno that you can download at

http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

3. In this case, there were two different objects to be accesssed by the 
same index number. That's why I've used seq_along(). Suppose the list 
length was zero. Then, 1:length(lst) would become the perfectly legal 
vector 1:0 producing the wrong result. See ?seq_along and seq_len.
They always produce the right vector.

Good luck,

Rui Barradas

Em 19-06-2012 09:26, Johannes Reichl escreveu:
> Yes this solution works great, thanks a lot! Sorry for not giving the
> example correctly.
> Could you point me to some advanced manual/tutorial for list operations?
> I am obviously still relying on loops too much....
> 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
> <mailto:reichl at energieinstitut-linz.at>
> Web: www.energieinstitut-linz.at <http://www.energieinstitut-linz.at/>
> www.energyefficiency.at <http://www.energyefficiency.at/>
>  >>> Rui Barradas <ruipbarradas at sapo.pt> 19.06.2012 08:18 >>>
> 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