FPL
FPL

Reputation: 466

Custom function in R with dplyr

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!

Upvotes: 1

Views: 192

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388797

Since functions with _ (like group_by_, count_ etc) are deprecated, you can use -

library(dplyr)

value_count <- function(data, group_col) {
  data %>%
    count({{group_col}}, name = 'count') %>%
    mutate(count = prop.table(count) * 100)
}

value_count(data, colour)

Upvotes: 2

TarJae
TarJae

Reputation: 78907

There is a typo mutate_ should be mutate

value_count2 = function(data, group_col) {
    data %>% 
        group_by_(.dots = lazyeval::lazy(group_col)) %>% 
        summarize(count=n()) %>% 
        mutate(percent = count/(sum(count)) * 100)
}

Test with mtcars:

value_count2 = function(data, group_col) {
    data %>% 
        group_by_(.dots = lazyeval::lazy(group_col)) %>% 
        summarize(count=n()) %>% 
        mutate(percent = count/(sum(count)) * 100)
}

value_count2(mtcars, cyl)

output:

    cyl count percent
  <dbl> <int>   <dbl>
1     4    11    34.4
2     6     7    21.9
3     8    14    43.8

Upvotes: 1

Related Questions