Using a variable number of groups with do in function
70 观看
2回复
450 作者的声誉
I would like to understand if and how this could be achieved using the tidyverse framework.
Assume I have the following simple function:
my_fn <- function(list_char) {
data.frame(comma_separated = rep(paste0(list_char, collapse = ","),2),
second_col = "test",
stringsAsFactors = FALSE)
}
Given the below list:
list_char <- list(name = "Chris", city = "London", language = "R")
my function works fine if you run:
my_fn(list_char)
However if we change some of the list's elements with a vector of characters we could use the dplyr::do
function in the following way to achieve the below:
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")))
The question is how to write a function that could do this for a list with a variable number of elements. For example:
my_fn_generic <- function(list_char_mult) {
expand.grid(list_char_mult, stringsAsFactors = FALSE) %>%
tbl_df() %>%
group_by_all() %>%
do(my_fn(...))
}
Thanks
作者: Christos 的来源 发布者: 2017 年 12 月 27 日回应 2
1像
64675 作者的声誉
If I understand your question, you could use apply
without grouping:
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))
作者: eipi10 发布者: 2017 年 12 月 27 日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
1像
474039 作者的声誉
Regarding how to use the function with variable number of arguments
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
Or use the 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
作者: akrun
发布者: 2017 年 12 月 27 日
来自类别的问题 :
- r 如何访问向量中的最后一个值?
- r R的优化包
- r R是否有类似引用的运算符,如Perl的qw()?
- r R中没有标题/标签的图
- r 计算移动平均线
- function How do you pass a function as a parameter in C?
- function JavaScript闭包如何工作?
- function python中有一个函数将一个单词拆分成一个列表吗?
- function 哪个更适合在Python中使用:lambda函数或嵌套函数('def')?
- function Is there a better way to do optional function parameters in JavaScript?
- dplyr 使用字符串向量输入在dplyr中按多列分组
- dplyr dplyr可以汇总几个变量而不列出每个变量吗?
- dplyr dplyr filter:获取具有最小变量的行,但仅获取第一个if多个最小值
- dplyr 在dplyr中替换“重命名”
- dplyr 提取dplyr tbl列作为向量
- tidyverse 为列表 - 列数据框的每一行安装不同的模型
- tidyverse 带有小标题列表的列中的最后一个对象结转
- tidyverse 使用tidyverse扩展数据框
- tidyverse 小标题中的列表列:我可以将一个列表列与另一个列表列链接吗?
- tidyverse 使用map和purrr在listcolumn中分配NA