Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a string to dplyr filter in a function?

I am looking for a way to pass a string as an input to the filter_ function in dplyr package within my own function. I have set up the data frame as follows:

df = data.frame(
    X1 = LETTERS[1:5], 
    X2 = c("apple", "apple", "apple", "banana", "banana")
)

I am looking for a way to write a function in which I can pass either "apple" or "banana" to filter the data frame.

I have tried:

filterFruit = function(Data, Fruit){
   retVal = filter_(Data, "X2 == Fruit")
   return(retVal)
}

Then passing the values :

apple1 = filterFruit(df, "apple")
apple1

This would returne an error:

Error: object 'Fruit' not found

I have tried a few other ways of doing this with no success, I hope that someone could help.

Edit:

I have realised that I do not need to use filter_ for this operation as it I am not selecting which column I am filtering by and can just pass the arguments into filter with no quotes. The question still stands however for the case in which you have:

df = data.frame(
    X1 = LETTERS[1:5], 
    X2 = c("apple", "apple", "apple", "banana", "banana")
    X3 = c("apple", "banana", "apple", banana", "apple")
)

and need to decide which column (X2 or X3) that you need to filter by.

like image 474
Jamesm131 Avatar asked Feb 07 '17 22:02

Jamesm131


Video Answer


2 Answers

This should answer your edit:

library(dplyr)

df = data.frame(
  X1 = LETTERS[1:5], 
  X2 = c("apple", "apple", "apple", "banana", "banana"),
  X3 = c("apple", "banana", "apple", "banana", "apple"), 
  stringsAsFactors=FALSE
)

column_string = "X2"
column_value = "banana"
column_name <- rlang::sym(column_string)

filtered_df <- df %>%
  filter(UQ(column_name) == UQ(column_value))

filtered_df
like image 141
Tom Roth Avatar answered Sep 28 '22 22:09

Tom Roth


I've used which to get the fruits. To get just the index, you can use retVal.

filterFruit <-  function(Data, column, Fruit){
  idx <- Data[,column]
  retVal <-  which(idx == Fruit)
  fruits_here <- Data[c(retVal), column]
  return(fruits_here)
}
like image 39
ronnie Avatar answered Sep 29 '22 00:09

ronnie