在函数中使用可变数量的组

Using a variable number of groups with do in function

我想了解是否以及如何使用 tidyverse 框架实现这一目标。

假设我有以下简单函数:

my_fn <- function(list_char) {
  data.frame(comma_separated = rep(paste0(list_char, collapse = ","),2), 
         second_col = "test", 
         stringsAsFactors = FALSE)
}

给定以下列表:

list_char <- list(name = "Chris", city = "London", language = "R")

如果你 运行:

我的函数工作正常
my_fn(list_char)

但是,如果我们用字符向量更改列表的某些元素,我们可以按以下方式使用 dplyr::do 函数来实现以下目的:

list_char_mult <- list(name = c("Chris", "Mike"),
                       city = c("New York", "London"), language = "R")

expand.grid(list_char_mult, stringsAsFactors = FALSE) %>%
  tbl_df() %>%
  group_by_all() %>% 
  do(my_fn(list(name = .$name, city = .$city, language = "R")))

问题是如何编写一个函数来对元素数量可变的列表执行此操作。例如:

my_fn_generic <- function(list_char_mult) {
  expand.grid(list_char_mult, stringsAsFactors = FALSE) %>%
    tbl_df() %>%
    group_by_all() %>% 
    do(my_fn(...))
}

谢谢

如果我理解你的问题,你可以使用 apply 而不分组:

expand.grid(list_char_mult, stringsAsFactors = FALSE) %>%
  mutate(comma_separated = apply(., 1, paste, collapse=",")) 

expand.grid(list_char_mult, stringsAsFactors = FALSE) %>%
  mutate(comma_separated = apply(., 1, my_fn)) 
   name     city language  comma_separated
1 Chris   London        R   Chris,London,R
2 Chris New York        R Chris,New York,R
3  Mike   London        R    Mike,London,R
4  Mike New York        R  Mike,New York,R

关于如何使用参数个数可变的函数

my_fn_generic <- function(list_char) {
  expand.grid(list_char, stringsAsFactors = FALSE) %>%
    tbl_df() %>%
    group_by_all() %>% 
    do(do.call(my_fn, list(.)))
 }
my_fn_generic(list_char_mult)
# A tibble: 4 x 4
# Groups: name, city, language [4]
#   name  city     language comma_separated 
#  <chr> <chr>    <chr>    <chr>           
#1 Chris London   R        Chris,London,R  
#2 Chris New York R        Chris,New York,R
#3 Mike  London   R        Mike,London,R   
#4 Mike  New York R        Mike,New York,R 

或使用pmap

library(tidyverse)
list_char_mult %>%
     expand.grid(., stringsAsFactors = FALSE) %>%
     mutate(comma_separated = purrr::pmap_chr(.l = ., .f = paste, sep=", ") )
#  name     city language     comma_separated
#1 Chris New York        R Chris, New York, R
#2  Mike New York        R  Mike, New York, R
#3 Chris   London        R   Chris, London, R
#4  Mike   London        R    Mike, London, R