[R] How to iteratively extract elements out of a list
Patrick Connolly
p_connolly at ihug.co.nz
Sun Aug 27 01:55:03 CEST 2006
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)]
etc.
--
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
___ Patrick Connolly
{~._.~} Great minds discuss ideas
_( Y )_ Middle minds discuss events
(:_~*~_:) Small minds discuss people
(_)-(_) ..... Anon
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
More information about the R-help
mailing list