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.
This should answer your edit: