Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering a data frame

Tags:

select

r

filter

I have read in a csv file in matrix form (having m rows and n columns). I want to filter the matrix by conducting a filter in verbal form:

Select all values from column x where the values of an another column in this row is equal to "blabla".

It is like a select statement in database where I say I am interested in a subset of the matrix where these constraints need to be satisfied.

How can I do it in r? I have the data as dataframe and can access it by the headers. data["column_values" = "15"] does not give me back the rows where the column named column_values have values 15 only.

Thanks

like image 347
Bob Avatar asked Sep 23 '11 20:09

Bob


1 Answers

You said you just wanted the column x values where column_values was 15, right?

subset(dat, column_values==15, select=x)

I think this may come as a dataframe so it's possble you may need to unlist() it and maybe even "unfactor" it.

> dat
  Subject Product
1       1   ProdA
2       1   ProdB
3       1   ProdC
4       2   ProdB
5       2   ProdC
6       2   ProdD
7       3   ProdA
8       3   ProdB
> subset(dat, Subject==2, Product)
  Product
4   ProdB
5   ProdC
6   ProdD
> unlist( subset(dat, Subject==2, Product) )
Product1 Product2 Product3 
   ProdB    ProdC    ProdD 
Levels: ProdA ProdB ProdC ProdD
> as.character( unlist( subset(dat, Subject==2, Product) ) )
[1] "ProdB" "ProdC" "ProdD"

If you want all of the columns you can drop the third argument (the select= argument):

subset(dat, Subject==2 )

  Subject Product
4       2   ProdB
5       2   ProdC
6       2   ProdD
like image 188
IRTFM Avatar answered Sep 28 '22 19:09

IRTFM