[R] Subsetting with a list of vectors
Kang Min
ngokangmin at gmail.com
Sun May 23 16:00:40 CEST 2010
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
More information about the R-help
mailing list