Re: [R] How to iteratively extract elements out of a list

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sun 27 Aug 2006 - 10:23:07 EST

On 8/26/06, Patrick Connolly <p_connolly@ihug.co.nz> wrote:
> On Sat, 26-Aug-2006 at 09:57AM +0100, Patrick Burns wrote:
>
> |> > sub.m <- lapply(m, function(x)x[x>2])
> |> > sub.m
> |> [[1]]
> |> [1] 3 4
> |>
> |> [[2]]
> |> [1] 4 5
> |>
> |> [[3]]
> |> [1] 4
> |>
> |> > sub.m[unlist(lapply(sub.m, function(x) length(x) == 2))]
> |> [[1]]
> |> [1] 3 4
> |>
> |> [[2]]
> |> [1] 4 5
> |>
> |> > sub4.m <- lapply(m, function(x)x[x>4])
> |> > sub4.m[unlist(lapply(sub4.m, function(x) length(x) > 0))]
> |> [[1]]
> |> [1] 5
>
> Or slightly shorter in this case:
>
> sub.m[sapply(sub.m, function(x) length(x) == 2)]
>

or even shorter:

sub.m[sapply(sub.m, length) == 2]



R-help@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. Received on Sun Aug 27 10:26:19 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 27 Aug 2006 - 12:27:23 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.