1

I have dataframe "fish" which has 3 columns. The dataframe is sorted by values in column 1. I want to select the rows based on the lowest values in column 3. How do I select only these rows?? I am trying to graph the data in column 1 by the lowest column 3 values.

kiki33
  • 13
  • 2
  • 1
    More info is needed. – Evan Carslake Sep 18 '15 at 22:18
  • Welcome to StackOverflow, @kiki33! When asking volunteers to help you with code and/or problems, we are neither mind-readers nor do we have the time to contrive our own data in order to possibly-match your intent. To help us help you, **please read** about [minimal, complete, verifiable examples](http://stackoverflow.com/help/mcve) and [reproducible examples](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). After that, come back here and edit your question accordingly. (Until then, you will likely get little or no reply.) – r2evans Sep 18 '15 at 23:18

1 Answers1

0

This code creates a dataframe and returns the rows with the three lowest values for the column we've called unif. You'll already have the dataframe, so you just need to select the column you want to filter based on. Where I've used unif, you'd use whatever your column's name is.

## create the dataframe
n = 10 
df = data.frame(round(runif(n),1), round(rnorm(n),1))
colnames(df) = c('unif', 'norm')
   unif norm
1   0.4  0.7
2   0.4  0.2
3   0.3  1.3
4   0.8 -0.4
5   0.3 -0.6
6   0.3  1.8
7   0.5 -1.0
8   0.4  0.4
9   0.0  0.2
10  0.6 -0.6

With that dataframe, we just need to filter the rows to the rows with the lowest three values on the unif column.

## return the rows with the three lowest values 
df[order(df$unif)[(1:3],]   #### This is the part you need

Will return this result, which is I think what you want:

   unif norm
7   0.5 -1.0
10  0.6 -0.6
4   0.8 -0.4
Hatshepsut
  • 5,962
  • 8
  • 44
  • 80