# [R] Subsetting with a list of vectors

jim holtman jholtman at gmail.com
Sun May 23 16:12:57 CEST 2010

```try this:

> x <- read.table(textConnection("plot     plantno.    species
+ H          31             ABC
+ D          2               DEF
+ Y          54             GFE
+ E          12             ERF
+ Y          98             FVD
+ H          4               JKU
+ J           7               JFG
+ A          55             EGD"), header=TRUE, as.is=TRUE)
> closeAllConnections()
> # chose 10 groups of 3 sample
> choice <- lapply(1:10, function(.dummy){
+     x[sample(nrow(x),3),]
+ })
>
> choice
[[1]]
plot plantno. species
3    Y       54     GFE
8    A       55     EGD
4    E       12     ERF

[[2]]
plot plantno. species
8    A       55     EGD
2    D        2     DEF
6    H        4     JKU

[[3]]
plot plantno. species
8    A       55     EGD
5    Y       98     FVD
4    E       12     ERF

........

On Sun, May 23, 2010 at 10:00 AM, Kang Min <ngokangmin at gmail.com> wrote:
> Hi,
>
> I have a dataset that looks like the one below.
>
> data
> plot     plantno.    species
> H          31             ABC
> D          2               DEF
> Y          54             GFE
> E          12             ERF
> Y          98             FVD
> H          4               JKU
> J           7               JFG
> A          55             EGD
> .            .                 .
> .            .                 .
> .            .                 .
>
> I want to select rows belonging to 7 random plots for 100 times.
> (There are 50 plots in total)
> So I created a list of 100 vectors, each vector has 7 elements.
>
> samp <- lapply(1:100, function(i) sample(LETTERS))
> samp2 <- lapply(samp2, "[", 1:7)
>
> How can I select the 26 plots from 'data' using 'samp'?
>
> samp3 <- sample(LETTERS, 7)
> samp4 <- subset(data, plot %in% samp3) # this works
> samp5 <- subset(data, plot %in% samp2[[1]]) # this works as well, but
> I used a for loop to get it to select 7 plots 100 times.
>
> for (i in nrow(samp2)) {
>      samp6 <- subset(data, plot %in% samp2[[i]])
> } # this doesn't work
>
>
> Am I missing something, or is there a better solution?
>
> Thanks.
> Kang Min
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

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

What is the problem that you are trying to solve?

```