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

Christoph Scherber Christoph.Scherber at agr.uni-goettingen.de
Mon May 21 13:49:09 CEST 2007


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



More information about the R-help mailing list