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

From: Christoph Scherber <Christoph.Scherber_at_agr.uni-goettingen.de>
Date: Mon, 21 May 2007 13:49:09 +0200


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. Received on Mon 21 May 2007 - 11:51:51 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.