I have the following date.frame in R:
df <- data.frame(x = c(10,10,2,3,4,8,8,8),
y = c(5,4,6,7,8,3,2,4))
df
x y
1 10 5
2 10 4
3 2 6
4 3 7
5 4 8
6 8 3
7 8 2
8 8 4
First point : I would like to get all rows containing the top 5 values of the x
column,
Example:
The top five of the x
column are: 10, 10, 8, 8, 8.
I can get with the following code:
rev(sort(df$x))[1:5]
[1] 10 10 8 8 8
But I'd like to get the entire row, not just the values from the x
column. So the result I want is:
1 10 5
2 10 4
6 8 3
7 8 2
8 8 4
And not:
> [1] 10 10 8 8 8