I'm trying to write a custom function for R which has elements of dplyr in it.
The first part went well. I made a function, value_count that creates a frequency distribution table for any column in a specified data frame:
value_count = function(data, group_col) {
data %>%
group_by_(.dots = lazyeval::lazy(group_col)) %>%
summarize(count=n())
Here's an example of the output:
colour count
green 10
red 8
blue 2
Now, in the same function, I want to add a "percent" column that calculates the % of people who choose each colour. Like this:
colour count percent
green 10 50
red 8 40
blue 2 10
To do so, I tried this function:
value_count2 = function(data, group_col) {
data %>%
group_by_(.dots = lazyeval::lazy(group_col)) %>%
summarize(count=n()) %>%
mutate(percent = count/(sum(count)) * 100)
}
But I get this error:
"Error: Problem adding computed columns in `group_by()`.
x Problem with `mutate()` input `..1`.
I've tested this outside of creating a function (i.e. manually performing group_by(group_col) %>% summarize(count=n()) %>% mutate(percent = count/(sum(count)) * 100) on the same dataset) and everything works perfectly :-S
Any idea what is occurring? Thank you!