How to filter a data frame with dplyr and tidy evaluation in R?

0 votes

Let's say I want to filter the starwars data frame programmatically. Here's a simple example that lets me filter based on homeworld and species:

library(tidyverse)

# a function that allows the user to supply filters
filter_starwars <- function(filters) {
  for (filter in filters) {
    starwars = filter_at(starwars, filter$var, all_vars(. %in% filter$values))
  }

  return(starwars)
}

# filter Star Wars characters that are human, and from either Tatooine or Alderaan
filter_starwars(filters = list(
  list(var = "homeworld", values = c("Tatooine", "Alderaan")),
  list(var = "species", values = "Human")
))

But this doesn't let me specify, say, a height filter, because I've hard-coded the %in% operator in the .vars_predicate of filter_at(), and a height filter would use one of the >, >=, <, <=, or ==operators

What is the best way to write the filter_starwars() function so that the user can supply filters that are general enough to filter along any column and use any operator?

NB using the now-deprecated filter_() method, I could pass a string:

filter_(starwars, "species == 'Human' & homeworld %in% c('Tatooine', 'Alderaan') & height > 175")

But again, that has been deprecated.

May 16, 2018 in Data Analytics by darklord
• 6,140 points
50 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Requires the use of map_df to run each model, and tidy to extract the values from each model.

Hopefully this will be useful 

library(tidyverse)
library(broom)

df <- mtcars

output <- df %>%
  select(-am) %>%
  names() %>%
  paste('am~',.) %>%
  map_df(~tidy(glm(as.formula(.x), 
               data= df, 
               family = "binomial"), 
               conf.int=TRUE, 
               exponentiate=TRUE)) %>%
  filter(term !="(Intercept)")
answered May 16, 2018 by DataKing99
• 8,100 points

Related Questions In Data Analytics

0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
60 views
0 votes
1 answer

How to convert a list to data frame in R?

Let's assume your list of lists is ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,060 points

edited Apr 12, 2018 by nirvana 1,518 views
0 votes
1 answer

How to remove rows with missing values (NAs) in a data frame?

You can use complete.cases in the following ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
3,316 views
0 votes
1 answer

How to convert tables to a data frame in R ?

> trial.table.df <- as.data.frame(trial.table) //assuming that trial.table ...READ MORE

answered Apr 20, 2018 in Data Analytics by zombie
• 3,690 points
21 views
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
38 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 36,300 points
26 views
0 votes
1 answer
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,700 points
66 views
0 votes
1 answer

How to forecast season and trend of data using STL and ARIMA in R?

You can use the forecast.stl function for the ...READ MORE

answered May 18, 2018 in Data Analytics by DataKing99
• 8,100 points
413 views
0 votes
1 answer

How to evaluate expression given as a string in R?

The eval() function evaluates an expression, but "5+5" is a string, ...READ MORE

answered Jun 7, 2018 in Data Analytics by DataKing99
• 8,100 points
71 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.