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.
nis a 0-argument function that returns an error and that is assigned tonew_n. It seemsn()insummariseis not seen as a function but as a map to a specific operation. Similarly see, withrow_number = function() stop("")this still works:summarise(carriers, row_number())- alexis_laz