[R] Extracting elements from a list
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sat Jul 14 15:32:28 CEST 2007
The golden rule is that [[ ]] only returns one element:
sapply(Lst, "[", 1, 1)
is probably what you want.
On Sat, 14 Jul 2007, 'Forest Floor' aka 'rhago' aka 'Jeff' aka 'R User
confused about his identity' wrote:
> Hi,
>
> I would love an easy way to extract elements from a list.
>
> For example, if I want the first element from each of 10 arrays stored
> in a list,
>
> Lst[[1:10]][1,1] seems like a logical approach, but gives this error:
> "Error: recursive indexing failed at level 3"
It doesn't if you really have a list of 2D arrays.
> The following workaround is functional but can get annoying/confusing.
>
> first.element=vector()
> for (i in 1:10){ first.element=c(first.element, Lst[[i]][1,1]) }
>
> Is there a better way to do this? Thanks for any help!
>
> Jeff
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
PLEASE DO!
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list