I was thinking about create functions based on dplyr package. I have seen few examples, such as janitor package and Organism.dplyr. But, I don't know I can expand or inherent the dplyr features, or even if it is possible.
For instance. What I want:
data %>% group_by(columnX) %>% my_mutate_like_function()
But, It doesn't work, I saw a post about it using do() as an alternative... But, it is not what I want.
Could anyone help me? Thanks.
== Follows a code example (Edited) ==
data <- data.frame(groupname = c('A', 'B', 'A', 'A', 'B', 'B'),
value = c(1, 3, 4, 2, 1.4, 5))
my_mutate_like_function <- function(data) {
data$category <- ifelse(data$value <= mean(data$value), 'In', 'Out')
data$meanvalue <- mean(data$value)
data
}
data_works <- data %>% group_by(groupname) %>%
mutate(category = ifelse(value <= mean(value), 'In', 'Out'), meanvalue = mean(value))
# That's the right output, each "groupname" had their average calculated and it was used a threshold value
data_fails <- data %>% group_by(groupname) %>%
my_mutate_like_function()
# The group_by properties seems not work inside my function