Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a data frame

Tags:

r

r-faq

filter

I have a data frame and tried to select only the observations I'm interested in by this:

data[data["Var1"]>10] 

Unfortunately, this command destroys the data.frame structure and returns a long vector.

What I want to get is the data.frame shortened by the observations that don't match my criteria.

like image 451
Mike Avatar asked Aug 18 '11 11:08

Mike


People also ask

How do you filter rows in a data frame?

Filter Rows by Condition You can use df[df["Courses"] == 'Spark'] to filter rows by a condition in pandas DataFrame. Not that this expression returns a new DataFrame with selected rows.

Can you use filter on a data frame in R?

How to apply a filter on dataframe in R ? A filter () function is used to filter out specified elements from a dataframe that return TRUE value for the given condition (s). filter () helps to reduce a huge dataset into small chunks of datasets.


1 Answers

You are missing a comma in your statement.

Try this:

data[data[, "Var1"]>10, ] 

Or:

data[data$Var1>10, ] 

Or:

subset(data, Var1>10) 

As an example, try it on the built-in dataset, mtcars

data(mtcars)  mtcars[mtcars[, "mpg"]>25, ]                  mpg cyl  disp  hp drat    wt  qsec vs am gear carb Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1 Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2 Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1 Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2 Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2   mtcars[mtcars$mpg>25, ]                  mpg cyl  disp  hp drat    wt  qsec vs am gear carb Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1 Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2 Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1 Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2 Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2  subset(mtcars, mpg>25)                  mpg cyl  disp  hp drat    wt  qsec vs am gear carb Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1 Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2 Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1 Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2 Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2 
like image 95
Andrie Avatar answered Sep 27 '22 20:09

Andrie