2017-12-27 433 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