[R] Subsetting with a list of vectors

Kang Min ngokangmin at gmail.com
Sun May 23 16:38:14 CEST 2010


Thanks, but what I want is not 100 groups of 7 samples. Let's say in
my samp2 I get

[[1]] "D" "H" "K" "S" "E" "U" "O"
[[2]] "H" "S" "R" "V" "A" "L" "B"
etc...

I want to select all rows from 'data' containing "D" "H" "K" "S" "E"
"U" "O" first, then "H" "S" "R" "V" "A" "L" "B" and so on.



On May 23, 10:12 pm, jim holtman <jholt... at gmail.com> wrote:
> 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 <ngokang... 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-h... at r-project.org mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> > 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?
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> --
> You received this message because you are subscribed to the Google Groups "R-help-archive" group.
> To post to this group, send email to r-help-archive at googlegroups.com.
> To unsubscribe from this group, send email to r-help-archive+unsubscribe at googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/r-help-archive?hl=en.



More information about the R-help mailing list