[R] randomly subsample rows from subsets

Rui Barradas rui1174 at sapo.pt
Mon Mar 19 02:43:51 CET 2012


Hello,

Try

text="
fish fam length
1 a 71.46
2 a 71.06
3 a 62.94
4 b 79.46
5 b 52.38
6 b 56.78
7 b 92.08
8 c 96.86
9 d 98.09
10 d 17.23
11 d 98.35
12 d 82.43
13 e 83.85
14 e 33.92
15 e 23.16
16 e 31.39
17 e 57.08
18 e 27.05
19 f 62.38
20 f 83.21
21 f 18.72
22 f 84.32
23 g 15.99
24 h 40.33
25 h 92.73
26 h 59.08
27 i 29.05
"
fish <- read.table(textConnection(text), header=TRUE)
head(fish)

set.seed(1)
select <- lapply(split(fish, fish$fam),
	function(x) if(NROW(x) > 1) x[sample(NROW(x), 2), ])
select <- select[!sapply(select, is.null)]

# result as a list
select
# result as a data.frame
do.call(rbind, select)

Hope this helps,

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/randomly-subsample-rows-from-subsets-tp4483477p4483613.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list