Multiple assignment in dplyr::mutate()

I think the approach with enframe is rather specific, as it is rowise and you don't need to know the number of element in you complex element. It should apply to all list columns that you want to transform.

  1. Name the vector element in the list column
  2. enframe to get a tibble (this is the step that put it rowise)
  3. unnest the result
  4. spread as desired.

It is not specific to t.test or a two element list.

I think to approach this colwise, it is as @rensa said and gave as example, you need to know the number of element.

In fact, it the complex list is in the correct form you could use the splice operator !!! from rlang. The correct form is a list of desired column.

library(tidyverse)

# takes a column and format result in a list of desired column
conf_int_function <- function(column) {
  res <- map(column, ~ t.test(.x, conf.level = 0.95))
  conf <- map(res, "conf.int") %>% map(set_names, c('low', 'high'))
  transpose(conf) %>% simplify_all()
}

tibble(a = list(c(1, 2), c(3, 4))) %>%
  # use the splice operator to get the columns
  # You need `.$a` - it is not working with tidyeval
  mutate(!!!conf_int_function(.$a))
#> # A tibble: 2 x 3
#>   a           low  high
#>   <list>    <dbl> <dbl>
#> 1 <dbl [2]> -4.85  7.85
#> 2 <dbl [2]> -2.85  9.85

Created on 2018-09-11 by the reprex package (v0.2.0).

3 Likes