Using purrr::pmap within mutate to create list-column

前端 未结 4 2266
攒了一身酷
攒了一身酷 2021-01-04 08:20

I understand how to use map to iterate over arguments in a df and create a new list column.

For example,

params <- expand.grid(param_a = c(2, 4,          


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 08:32

    How about using rowwise and mutate directly without map:

    my_fun <- function(param_a, param_b){
      rep(5, param_a) * param_b
    }
    df.preprocessed <- dplyr::as.tbl(params) %>%
      rowwise() %>% 
      dplyr::mutate(test_var = list(my_fun(param_a, param_b))) %>% 
      tidyr::unnest()
    

提交回复
热议问题