Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between subset and filter from dplyr

Tags:

r

filter

subset

It seems to me that subset and filter (from dplyr) are having the same result. But my question is: is there at some point a potential difference, for ex. speed, data sizes it can handle etc? Are there occasions that it is better to use one or the other?

Example:

library(dplyr)  df1<-subset(airquality, Temp>80 & Month > 5) df2<-filter(airquality, Temp>80 & Month > 5)  summary(df1$Ozone) # Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's  # 9.00   39.00   64.00   64.51   84.00  168.00      14   summary(df2$Ozone) # Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's  # 9.00   39.00   64.00   64.51   84.00  168.00      14  
like image 642
Ruthger Righart Avatar asked Oct 05 '16 19:10

Ruthger Righart


People also ask

What does dplyr filter do?

The filter() function is used to subset a data frame, retaining all rows that satisfy your conditions. To be retained, the row must produce a value of TRUE for all conditions.

How do you differentiate among select and filter in R programming?

filter() operates on rows, whereas select() operates on columns. For example, in the reprex below, I'm using the built-in mtcars dataset to illustrate using filter() to retain certain rows by a certain criterion of interest, or using select() to retain certain columns based on column names.

What is the use of subset () and sample () function in R?

The difference between subset () function and sample () is that, subset () is used to select data from the dataset which meets certain condition, while sample () is used for randomly selecting data of size 'n' from the dataset.


2 Answers

They are, indeed, producing the same result, and they are very similar in concept.

The advantage of subset is that it is part of base R and doesn't require any additional packages. With small sample sizes, it seems to be a bit faster than filter (6 times faster in your example, but that's measured in microseconds).

As the data sets grow, filter seems gains the upper hand in efficiency. At 15,000 records, filter outpaces subset by about 300 microseconds. And at 153,000 records, filter is three times faster (measured in milliseconds).

So in terms of human time, I don't think there's much difference between the two.

The other advantage (and this is a bit of a niche advantage) is that filter can operate on SQL databases without pulling the data into memory. subset simply doesn't do that.

Personally, I tend to use filter, but only because I'm already using the dplyr framework. If you aren't working with out-of-memory data, it won't make much of a difference.

library(dplyr) library(microbenchmark)  # Original example microbenchmark(   df1<-subset(airquality, Temp>80 & Month > 5),   df2<-filter(airquality, Temp>80 & Month > 5) )  Unit: microseconds    expr     min       lq     mean   median      uq      max neval cld  subset  95.598 107.7670 118.5236 119.9370 125.949  167.443   100  a   filter 551.886 564.7885 599.4972 571.5335 594.993 2074.997   100   b   # 15,300 rows air <- lapply(1:100, function(x) airquality) %>% bind_rows  microbenchmark(   df1<-subset(air, Temp>80 & Month > 5),   df2<-filter(air, Temp>80 & Month > 5) )  Unit: microseconds    expr      min        lq     mean   median       uq      max neval cld  subset 1187.054 1207.5800 1293.718 1216.671 1257.725 2574.392   100   b  filter  968.586  985.4475 1056.686 1023.862 1036.765 2489.644   100  a   # 153,000 rows air <- lapply(1:1000, function(x) airquality) %>% bind_rows  microbenchmark(   df1<-subset(air, Temp>80 & Month > 5),   df2<-filter(air, Temp>80 & Month > 5) )  Unit: milliseconds    expr       min        lq     mean    median        uq      max neval cld  subset 11.841792 13.292618 16.21771 13.521935 13.867083 68.59659   100   b  filter  5.046148  5.169164 10.27829  5.387484  6.738167 65.38937   100  a  
like image 125
Benjamin Avatar answered Sep 28 '22 19:09

Benjamin


One additional difference not yet mentioned is that filter discards rownames, while subset doesn't:

filter(mtcars, gear == 5)    mpg    cyl   disp      hp  drat wt    qsec  vs am   gear carb 1 26.0   4     120.3     91  4.43 2.140 16.7  0  1    5    2 2 30.4   4     95.1      113 3.77 1.513 16.9  1  1    5    2 3 15.8   4     351.0     264 4.22 3.170 14.5  0  1    5    4 4 19.7   4     145.0     175 3.62 2.770 15.5  0  1    5    6 5 15.0   4     301.0     335 3.54 3.570 14.6  0  1    5    8  subset(mtcars, gear == 5)                mpg    cyl   disp      hp  drat wt    qsec vs  am   gear carb Porsche 914-2  26.0   4     120.3     91  4.43 2.140 16.7  0  1    5    2 Lotus Europa   30.4   4     95.1      113 3.77 1.513 16.9  1  1    5    2 Ford Pantera L 15.8   4     351.0     264 4.22 3.170 14.5  0  1    5    4 Ferrari Dino   19.7   4     145.0     175 3.62 2.770 15.5  0  1    5    6 Maserati Bora  15.0   4     301.0     335 3.54 3.570 14.6  0  1    5    8 
like image 32
rsmith54 Avatar answered Sep 28 '22 19:09

rsmith54