Message-ID: <491F663B242841E6A69F3A7DEB73F2CD@Aragorn>
Date: 2010-12-08T22:46:17Z
From: Daniel Nordlund
Subject: How can i select a set of element in a list ?
In-Reply-To: <AANLkTikFPLt5Pi5ZpP41uqLB8LvoG1ioZ8CZUTK2KZJp@mail.gmail.com>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Julian TszKin Chan
> Sent: Wednesday, December 08, 2010 2:39 PM
> To: r-help at r-project.org
> Subject: [R] How can i select a set of element in a list ?
>
> How can i select a set of element in a list ?
>
> x<-list(1,2,3,4,5)
>
> How can I create a new list which only have the last 4 elements of x.
> I can do it with a loop, but I want to avoid that. Thanks!
>
> example :
> y<-vector("list",4)
> for ( i in 1:length(y) )
> y[[i]] <- x[[ i + length(x) - length(y) ]]
>
> > y
> [[1]]
> [1] 2
>
> [[2]]
> [1] 3
>
> [[3]]
> [1] 4
>
> [[4]]
> [1] 5
>
> Regards,
> Julian
>
Julian,
You use standard indexing to do that.
y <- x[-1]
Hope this is helpful,
Dan
Daniel Nordlund
Bothell, WA USA