[R] Extract Row on Criteria
David Winsemius
dwinsemius at comcast.net
Fri Mar 6 04:38:33 CET 2009
See if this gets you further along:
?order
?rev
?"["
> df <- data.frame(x = 1:20, rsamp = sample(1:20,20), Lets
=LETTERS[1:20])
> ord <- order(df$rsamp)
# creates a sorted vector of row numbers
> df[ord,]
x rsamp Lets
16 16 1 P
14 14 2 N
18 18 3 R
15 15 4 O
1 1 5 A
3 3 6 C
9 9 7 I
19 19 8 S
20 20 9 T
10 10 10 J
7 7 11 G
17 17 12 Q
13 13 13 M
8 8 14 H
11 11 15 K
12 12 16 L
2 2 17 B
6 6 18 F
4 4 19 D
5 5 20 E
#rev ... reverses
> df[rev(ord),]
x rsamp Lets
5 5 20 E
4 4 19 D
6 6 18 F
2 2 17 B
12 12 16 L
11 11 15 K
8 8 14 H
13 13 13 M
17 17 12 Q
7 7 11 G
10 10 10 J
20 20 9 T
19 19 8 S
9 9 7 I
3 3 6 C
1 1 5 A
15 15 4 O
18 18 3 R
14 14 2 N
16 16 1 P
# successive applications of [ , ][ , ] can be very powerful
# this is the first 10 of the reverse sorted by rsamp df
> df[rev(ord),][1:5,]
x rsamp Lets
5 5 20 E
4 4 19 D
6 6 18 F
2 2 17 B
12 12 16 L
>
And next time, ... please use dput( ) for offering a sample dataframe.
--
David Winsemius
On Mar 5, 2009, at 9:57 PM, Bob Roberts wrote:
> Hi,
> I have a data frame and I want to extract the entire rows that
> have the 20 largest numbers in column Z. How would I go about doing
> that? Thanks.
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
More information about the R-help
mailing list