using purrr map

hi here is my code:

my_function <- function(df, group_var, summary_var) { 
df %>% 
group_by(!!sym(group_var)) %>% 
summarize(total = sum(!!sym(summary_var))) %>%
ungroup()

}

data_loop <- purrr::map(.f = my_function, df = my_data, group_var = var_list[3:4], summary_var = var_list[3:4])

error: argument .x is missing, with no default

anyone can assist me please when I try to run the loop thank you!

You are missing the .x argument. You want to map over something (such as the list of column names). Perhaps provide an example dataset to make it clearer.

1 Like

I want to loop over the list of column names. hence, why I have var_list[3:4].. but I want to use that for both arguments. it only works if I set group_var or summary_var

data_loop <- purrr::map(.f = my_function, df = my_data, group_var = "color", .x = var_list[3:4])

this works but I want to loop across group_var too

How does that work? There is no .x argument.

Again, a reproducible example would be good if you want to make it easier for people to help you.

1 Like
structure(list(animal = c("rhino", "rhino", "monkey", "cat", 
"cat", "giraffe"), pink = c(TRUE, FALSE, FALSE, FALSE, TRUE, 
FALSE), count = c(44, 11, 223, 1233, 23, 88), happiness_point = c(1, 
3, 2, 3, 2, 3)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

my_function <- function(df,  count_var, group_var) { 
df %>% 
count(!!sym(count_var)) %>%
group_by(!!sym(group_var)) %>% 
mutate(percent = n/sum(n)) %>%
ungroup()

}
var_list <- c("dog", "blue", "pink", "animal")

data_loop <- purrr::map(.f = my_function, df = my_data, group_var = var_list[3:4], summary_var = var_list[3:4])

here is the data above. sometimes count_var and group_var will be different but in this situation its the same I know. I just want to have them as Different inputs. thank u so much !

What are you trying to do in your function?

# A tibble: 6 x 4
  animal  pink  count happiness_point
  <chr>   <lgl> <dbl>           <dbl>
1 rhino   TRUE     44               1
2 rhino   FALSE    11               3
3 monkey  FALSE   223               2
4 cat     FALSE  1233               3
5 cat     TRUE     23               2
6 giraffe FALSE    88               3

Your my_function() doesn't work with this data. Are you trying to filter then count? None of the examples in var_list are in my_data.

sorry. I am trying to essentially count by a variable I select (pink) and then group by a variable I select (pink) and then calculate percent. I then want to run a loop and run this same thing for "animal". I count by "animal" and then group by "animal". and then it makes a list with 2 elements

structure(list(animal = c("rhino", "rhino", "monkey", "cat", 
"cat", "giraffe"), pink = c(TRUE, FALSE, FALSE, FALSE, TRUE, 
FALSE), count = c(44, 11, 223, 1233, 23, 88), happiness_point = c(1, 
3, 2, 3, 2, 3)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

my_function <- function(df,  count_var, group_var) { 
df %>% 
count(!!sym(count_var)) %>%
group_by(!!sym(group_var)) %>% 
mutate(percent = n/sum(n)) %>%
ungroup()

}
var_list <- c("dog", "blue", "pink", "animal")

data_loop <- purrr::map(.f = my_function, df = my_data, count_var = var_list[3:4], group_var = var_list[3:4])

No idea if this is what you are after, but this works.

my_func <- function(dataset, col_name){
  
  dataset %>% 
    group_by(.data[[col_name]]) %>% 
    summarise(count = n()) %>% 
    mutate(pct = count / sum(count))
}

# for one
my_func(my_data, "pink") 


# for many
variables <- c("animal", "pink")

map(variables %>% 
      set_names(),
    ~my_func(my_data, .x))

# # output -------------
# $animal
# # A tibble: 4 x 3
# animal  count   pct
# <chr>   <int> <dbl>
#   1 cat         2 0.333
# 2 giraffe     1 0.167
# 3 monkey      1 0.167
# 4 rhino       2 0.333
# 
# $pink
# # A tibble: 2 x 3
# pink  count   pct
# <lgl> <int> <dbl>
#   1 FALSE     4 0.667
# 2 TRUE      2 0.333

Otherwise, create a function that works for one, then turn it in to a purrr function.

I want to have two inputs however is the thing here

I want to run a loop with two input variables

my_function <- function(df,  count_var, group_var) { 
df %>% 
count(!!sym(count_var)) %>%
group_by(!!sym(group_var)) %>% 
mutate(percent = n/sum(n)) %>%
ungroup()

}

however

map(variables %>% 
      set_names(),
    ~ my_function(my_data, .x))

does not work when there is 2 input

But your function doesn't work anyway. Make it work first.

You can use map2() for two inputs and pmap() for more than two.

This topic was automatically closed 21 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.