[R] how to extract the elements of a list of vectors in a fixed position?

Rolf Turner r.turner at auckland.ac.nz
Thu Nov 15 20:58:26 CET 2007


On 16/11/2007, at 7:05 AM, carol white wrote:

> Hi,
> How is it possible to extract athe elements of a list of vectors in  
> a fixed position? suppose that I have a list of 2-element vectors,  
> how can I extract the 2nd element of all vectors in the list? Can  
> it be done with indexing and not by element name?
>
> Thanks
>
> carol
>
> So in this example, I want to extract 2 and 4
> v = list (c(1,2), c(3, 4))
>
>> v
> [[1]]
> [1] 1 2
>
> [[2]]
> [1] 3 4
>

	lapply(v,function(x){x[2]})

or

	unlist(lapply(v,function(x){x[2]}))

or (I can't resist)

	unlist(lapply(v,function(x,i){x[i]},i=2)) # For more flexibility.


		cheers,

			Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}



More information about the R-help mailing list