3
votes

Depending on some condition, I have to choose between using dplyr::n and an arbitrary function (say for instance a function that returns 2 whatever argument is given).

If I do the following:

new_n <- dplyr::n
new_n <- ifelse(is.null(k), new_n, my_new_n)
data <- data %>% group_by_(z) %>% mutate_(n = new_n)

If for instance dplyr::n gets assigned to new_n I get the error

Error: This function should not be called directly 

while I was expecting it to work normally as it would do if I had written

data <- data %>% group_by_(z) %>% mutate_(n = n())

Why is this happening? Is there a work around? Basically I need to assign a different value to the variable n within the data depending on k but I cannot change the part of code where the mutate is performed due to the project requirements.

EDIT: added simple example. For instance, if you try to run

if (require("nycflights13")) {
carriers <- group_by(flights, carrier)
summarise(carriers, n())
mutate(carriers, n = n())
filter(carriers, n() < 100)
}

everything works fine, however if you try to run

new_n <- n
summarise(carriers, new_n())

the code won't work and you'll get the error above even though what I did was just assigning n to new_n.

1
@akrun I did not included data, but you can try to run a "different version" of my problem with the example in the ?n help section. If you define new_n <- n and then run the example with new_n() instead of n(), you'll get the same error and the same problem I am trying to explain. - mickkk
@akrun I added a working example - mickkk
n is a 0-argument function that returns an error and that is assigned to new_n. It seems n() in summarise is not seen as a function but as a map to a specific operation. Similarly see, with row_number = function() stop("") this still works: summarise(carriers, row_number()) - alexis_laz
@alexis_laz thanks that explains it. I dug a little deeper and in fact n returns an error by default. Anyway I solved my problem with an if else and lazyeval::interp. Not that brilliant of a solution but it seems to work fine. - mickkk

1 Answers

3
votes

With mutate() you use n() but with mutate_() you use ~n()

So either use

data %>% group_by(z) %>% mutate(n = n())

or

data %>% group_by_(~z) %>% mutate_(n = ~n())