This may seem like a weird question, but is there a way to pass a value to filter() that basically does nothing?
data(cars)
library(dplyr)
cars %>% filter(speed==`magic_value_that_returns_cars?`)
And you'd get the whole data frame cars
back. I'm thinking that this could be useful in a shiny application, where the user would just need to choose the values he wants to filter by; for example the user could choose "Europe", "Africa" or "America" and behind the scenes thet data frame would get filtered and then a table with descriptive statistics for "Europe" would be returned (if the user chose "Europe"). But what if the user wants to have descriptive statistics without first filtering? Is there a value that we could pass to filter to «cancel» filter and pass the whole dataframe to summarise()?
There is no function to un-filter or clear filters or un-subset in dplyr. Instead, to keep all rows and perform a calculation on another specific subset, you can apply a conditional with ifelse().
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. Note that when a condition evaluates to NA the row will be dropped, unlike base subsetting with [ .
The filter() method in R is used to subset a data frame based on a provided condition. If a row satisfies the condition, it must produce TRUE . Otherwise, non-satisfying rows will return NA values. Hence, the row will be dropped.
The column will always be equal to itself, so
cars %>% filter(speed == speed)
will return the full data set.
Update: Turns out that won't work when there are NA
values in the data. Sorry I missed that. So, to make this answer correct I will urge you go with the recommendation of @konvas from the comments.
cars %>% filter(TRUE)
Of course there are other ways, but I think this is the best.
If you're applying this in a shiny application, here's an example of how you can filter by "nothing" if for instance, a user selects "All". The combined use of braces and if
else
will effectively allow you to skip the line where you apply filter
to the data set:
x <- "All"
cars %>%
{if(x!="All") filter(.,speed==x) else .} %>%
head()
# speed dist
# 1 4 2
# 2 4 10
# 3 7 4
# 4 7 22
# 5 8 16
# 6 9 10
# No data is filtered
x <- 7
cars %>%
{if(x!="All") filter(.,speed==x) else .} %>%
head()
# speed dist
# 1 7 4
# 2 7 22
# The data is filtered by x==7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With