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

From: jim holtman <jholtman_at_gmail.com>
Date: Sat 26 Aug 2006 - 07:26:38 EST

try this:

> set.seed(123)
> tmpf <- function() {
+ x <- rpois(rpois(1,4),2)
+ }
> n <- 3
> m <- replicate(n,tmpf())
> m
[[1]]
[1] 3 2 4

[[2]]
[1] 0 2 4 2 2 5 2

[[3]]
[1] 2 0 4 1 0

> lapply(m, function(x)x[x>2])

[[1]]
[1] 3 4

[[2]]
[1] 4 5

[[3]]
[1] 4

>

On 8/25/06, xpRt.wannabe <xprt.wannabe@gmail.com> wrote:
> Dear List,
>
> The following code produces a list, which is what I what:
>
> set.seed(123)
> tmpf <- function() {
> x <- rpois(rpois(1,4),2)
> }
> n <- 3
> m <- replicate(n,tmpf())
> m
>
> [[1]]
> [1] 3 2 4
>
> [[2]]
> [1] 0 2 4 2 2 5 2
>
> [[3]]
> [1] 2 0 4 1 0
>
>
> Now I need something that would to extract iteratively (or as many
> times as
> the size of 'n') the values that are greater than 2 in each component
> of
> 'm' into another list such that the sub-list would be:
>
> [[1]]
> [1] 3 4
>
> [[2]]
> [1] 4 5
>
> [[3]]
> [1] 4
>
> Below is what I tried:
>
> for(i in 1:3)
> sub.list <- lapply(m,subset,m[[i]]>2)
>
> > sub.list
>
> which gives me something different from what I want:
>
> [[1]]
> [1] 4
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 4
>
> Any help would be appreciated.
>
> > version
> _
> platform i386-pc-mingw32
> arch i386
> os mingw32
> system i386, mingw32
> status
> major 2
> minor 2.1
> year 2005
> month 12
> day 20
> svn rev 36812
> language R
>
> ______________________________________________
> 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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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 Sat Aug 26 07:32:33 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 Sat 26 Aug 2006 - 16:22:28 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.