I'm new to function writing so hopefully the below makes some sense.
I want to create a function which takes some arguments, which will be used to subset a data.frame. I have searched across the forums and found these Q&As interesting, but haven't been able to answer my question from the discussions:
The function I want to create will take a df, a column name, and a value to match in the rows of the column name. Here's my attempt which I can see to be wrong:
x <- data.frame("col1"=c("email","search","direct"),
"col2"=c("direct","email","direct"),
"col3"=c(10,15,27))
fun <- function(df,col,val) {
result <- subset(df, col==val)
return(result)
}
I want to pass in the df, x. A column name, let's say "col2". A value, let's say "email". My attempt to do so returns a 0-length df.
fun(x,"col2","email")
Clearly I'm doing something wrong... can anyone help?