Hello,
I want to create a function in order to make some count after grouping.
The function works as the code shows:
library(tidyverse)
base<-mtcars
resumen<-function(x,y){
base %>%
group_by({{x}},{{y}}) %>%
count()
}
resumen(carb,gear)
Howeverm, I can't use map2.
I try to perform a "loop" with map2 and I failed time after time.
var_x<-list("carb","cyl")
var_y<-list("gear","vs")
map2(var_x,var_y, resumen )
What am I doing wrong?
I used the forum to obtain the notation {{variable}} inside the function.
But I couldn't find an example using purrr.
Thanks for your replies and time.
Have a nice week, community.
Hi, maybe like this?
library(tidyverse)
base<-mtcars
var_x<-list("carb","cyl")
var_y<-list("gear","vs")
resumen<-function(x,y){
base %>%
group_by(.data[[x]], .data[[y]]) %>%
count()
}
map2(var_x,var_y, resumen)
# or maybe this
map2_df(var_x,var_y, resumen)
You're passing strings as column names, see the 'indirection' section here: Programming with dplyr • dplyr (tidyverse.org)
1 Like
This works great
map2(var_x,var_y, resumen)
Thanks for the link. I'm trying to write some custom functions using dplyr.
Thanks, williaml
1 Like
system
Closed
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.