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,170 points
139 views

1 answer to this question.

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,130 points

Related Questions In Data Analytics

0 votes
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21 in Data Analytics by anonymous
• 31,840 points
152 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 3,625 views
0 votes
2 answers

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

Hi, The below code returns rows without ...READ MORE

answered Aug 20 in Data Analytics by anonymous
• 31,840 points
5,676 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
106 views
+1 vote
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,720 points
131 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
• 42,540 points
62 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,720 points
161 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,130 points
591 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,130 points
240 views