2017-12-27 276 views
5

我想了解是否以及如何使用tidyverse框架可以实现。使用可变数量的组在功能

假设我有以下简单功能:

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

鉴于以下列表:

my_fn(list_char) 

但是,如果我们改变:如果您运行

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

我的功能工作正常列表中的一些元素具有字符向量,我们可以使用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(...)) 
} 

感谢

回答

1

关于如何使用函数变量参数数量

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 
1

如果我理解你的问题,你可以使用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 
+0

这是一个非常有效的解决方案,但是,如果我有一个返回data.frame有超过一排的功能将无法正常工作。我会编辑我的问题,以确保今后明确。抱歉的混淆。 – Christos