Thank you to Dimitris Rizopoulos, Richard Heiberger, Patrick Burns and Bill
Dunlap for their answers. All are good. The solution I prefer is the one
from Bill Dunlap:
lst<-list(a=10,b=20)
lst[ is.element(names(lst), c("a", "c")) ]
$a:
[1] 10
This has the added virtue that no partial matching is done.
E.g., subscripting by name does the following and is.element
will not.
lst2 <- list(adam=1, catherine=3)
lst2[c("a","c")]
$adam:
[1] 1
$catherine:
[1] 3
On Mon, 23 Aug 2004, Patrick Joseph wrote:
> I have a list named lst which has an arbitrary number of named elements.
For
> example:
>
> >print(lst)
> $a:
> [1] 10
>
> $b:
> [1] 20
>
> I would like to extract the elements that match a vector of names but I
want
> the result contains only the matched elements. For example, if I do the
> following command:
>
> >lst.match _ lst[c("a","c")]
>
> I would like the result:
> $a:
> [1] 10
>
> and not:
> $a:
> [1] 10
>
>
> $"":
> NULL
_________________________________________________________________
Don't just Search. Find! http://search.sympatico.msn.ca/default.aspx The new
MSN Search! Check it out!
|