Bin
Bin

Reputation: 547

Function parameters for dplyr package

My codes are similar as follows:

output <- iris  %>%
  select(Sepal.Length, Sepal.Width, Species) %>%
  filter(Sepal.Width < 3)  %>%
  group_by(Species) %>%
  summarise( mean(Sepal.Length) )  %>%
  print
# works as expected

# But when I want to write a function like this:
output_function <- function(a, b, c) {
  out <- iris  %>%
    select(a, b, c) %>%
    filter(b < 3)  %>%
    group_by(c) %>%
    summarise( mean(a) )
  return(out)
} 

output_function(Sepal.Length, Sepal.Width, Species)
# does not work as expected

The reason is obvious, but I do not know how to solve it.
I do not know the variable types of the column variables when we use functions such as select, group_by, etc.
Thus, I do not know how to define correct parameters in this case so that they can be passed to the functions in dplyr.

Upvotes: 0

Views: 127

Answers (1)

saurav shekhar
saurav shekhar

Reputation: 606

  1. To extract name from stored in a string variable you will have to use as.name

    a<-"Col_Name"

    as.name(a) = Col_Name

  2. You cannot pass column names stored in a variable to conventional dplyr functions like select(), group_by(). You will have to use select_() and group_by_() instead

    a<- "Sepal.Length"

    select(iris, as.name(a)) #this will NOT work

    select_(iris, as.name(a)) #this will work

Try using these variants. Let me know if you have questions.

Upvotes: 1

Related Questions