Re: [R] Chosing a subset of a non-sorted vector

From: Adaikalavan Ramasamy <ramasamy_at_cancer.org.uk>
Date: Tue, 22 May 2007 10:38:43 +0100

You want to select two subplots for each DL value. Try:

  df <- data.frame( DL=gl(3,4), subplot=rep(1:4,3) )

  df$index <- 1:nrow(df)
  ind <- tapply( df$index, df$DL, function(x) sample(x,2) )   df[ unlist(ind), ]

You could also have used rownames(df) instead of creating df$index.

OR

   tmp <- lapply( split(df, df$DL), function(m) m[sample(1:nrow(m),2),] )    do.call("rbind", tmp)

Regards, Adai

Christoph Scherber wrote:
> Dear all,
>
> I have a tricky problem here:
>
> I have a dataframe with biodiversity data in which suplots are a
> repeated sequence from 1 to 4 (1234,1234,...)
>
> Now, I want to randomly pick two subplots each from each diversity level
> (DL).
>
> The problem is that it works up to that point - but if I try to subset
> the whole dataframe, I get stuck:
>
> DL=gl(3,4)
> subplot=rep(1:4,3)
> diversity.data=data.frame(DL,subplot)
>
>
> subplot.sampled=NULL
> for(i in 1:3)
> subplot.sampled=c(subplot.sampled,sort(sample(4,2,replace=F)))
>
> subplot.sampled
> [1] 3 4 1 3 1 3
> subplot[subplot.sampled]
> [1] 3 4 1 3 1 3
>
> ## here comes the tricky bit:
>
> diversity.data[subplot.sampled,]
> DL subplot
> 3 1 3
> 4 1 4
> 1 1 1
> 3.1 1 3
> 1.1 1 1
> 3.2 1 3
>
> How can I select those rows of diversity.data that match the exact
> subplots in "subplot.sampled"?
>
>
> Thank you very much for your help!
>
> Best wishes,
> Christoph
>
> (I am using R 2.4.1 on Windows XP)
>
>
> ##
> Christoph Scherber
> DNPW, Agroecology
> University of Goettingen
> Waldweg 26
> D-37073 Goettingen
>
> +49-(0)551-39-8807
>
> ______________________________________________
> R-help_at_stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>



R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 22 May 2007 - 09:45:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 May 2007 - 10:31:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.