I am still finding my way around tidy evaluation. One thing I struggle with is using the name passed into a function on the left hand side of a dplyr verb. Please can someone help on the basis of the example.
feel free to offer other improvements to style!
In the below, the mean passed back will always be in a column called outputcol. how do I change the below to pass it back using the same name as the column passed in (mass in the example)
# create a table of frequency of a category column as a percentage
# with optional summary of another column by named function
percentagetable <- function(df, category, summaryvar = NULL, summaryfunc = NULL) {
quocategory <- enquo(category)
hassummary <- !missing(summaryvar) & !missing(summaryfunc)
if (hassummary) {
quosummaryvar <- enquo(summaryvar)
summaryvarname <- quo_name(quosummaryvar)
outputsym <- rlang::ensym(summaryvarname)
}
dfout <- df %>%
group_by(!!quocategory)
if (hassummary) {
dfout <- dfout%>%
summarise (percent = 100 * n() / nrow(df), outputcol = summaryfunc(!!quosummaryvar))
} else {
dfout <- dfout %>%
summarise (percent = 100 * n() / nrow(df))
}
dfout
}
speciessummary <- percentagetable(starwars, species, mass, mean)
print(speciessummary)