Search code examples
rtidyversepurrrnames

Naming of elements in purrr:::map


In this question Name output of pmap on tibble I asked how to properly name the output of a pmap call and got a good answer, which I thought I understood.

When I further investigated this behaviour though, I am not so sure anymore that I understand the intrinsics:

library(purrr)
library(tibble)
params <- tibble(nm = LETTERS[1:2], x = 1:2, y = 2:3, z = 1:2)

## 1. setting names on the first argument gives the expected results
params %>%
  mutate(nm = set_names(nm)) %>% 
  pmap(function(nm, x, y, ...) {
    x + y
  }) %>%
  names()
# [1] "A" "B"


## 2. actually it does not need to be the first element
params %>%
  mutate(nm = set_names(nm)) %>% 
  pmap(function(x, y, nm, ...) {
    x + y
  }) %>%
  names()
# [1] "A" "B"

## 3. however, setting names on `x` does not work
params %>%
  mutate(x = set_names(x, nm)) %>% 
  pmap(function(x, y, nm, ...) {
    x + y
  }) %>%
  names()
# NULL

## 4. I thought maybe because they disappear during the addition `+`, but nope
params %>%
  mutate(z = set_names(z, nm)) %>% 
  pmap(function(x, y, nm, ...) {
    x + y
  }) %>%
  names()
# NULL

## 5. Ok maybe a catch all argument `...` prohibits this behaviour, but no
params %>%
  mutate(z = set_names(z, nm)) %>% 
  pmap(function(x, y, nm, z) {
    x + y
  }) %>%
  names()
# NULL

## 6. It seems that names on a character vector work even if not referenced directly
params %>%
  mutate(nm = set_names(nm)) %>% 
  pmap(function(x, y, ...) {
    x + y
  }) %>%
  names()
# [1] "A" "B"

Can somebody enlighten me how the names are ultimately determined in a [p]map call?


Solution

  • Answer taken from my comment on original question.

    Names are taken from the first column of your tibble/data.frame (which is structurally a list of vectors) and applied to the list of results (calls to the function).

    So it has almost nothing to do with how the function by itself compute the result.

    Steps are

    1. Extract names from first column of tibble/data.frame (for pmap) otherwise extract names from x
    2. Apply function to elements and collect results
    3. Name elements of result with names extracted in step 1