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

前端 未结 4 2299
攒了一身酷
攒了一身酷 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:36

    We could try

    f1 <- function(x, y, ...) rep(5, x)*y
    
    df.preprocessed <- dplyr::as.tbl(params) %>%
            dplyr::mutate(test_var = purrr::pmap(list(x = param_a
                                     ,y = param_b
                                     ,z = param_c
                                     ,u = param_d),f1
        )
       )
    df.preprocessed
    # A tibble: 36 x 5
    #   param_a param_b param_c param_d  test_var
    #                  
    # 1       2       3      50       1 
    # 2       4       3      50       1 
    # 3       6       3      50       1 
    # 4       2       6      50       1 
    # 5       4       6      50       1 
    # 6       6       6      50       1 
    # 7       2       9      50       1 
    # 8       4       9      50       1 
    # 9       6       9      50       1 
    #10       2       3     100       1 
    # ... with 26 more rows
    

提交回复
热议问题