Matthew Strasiotto
Matthew Strasiotto

Reputation: 369

purrr - pmap - Supplying different column names to function parameter names in tidy style

I'm wondering if there's a way to specify which columns should be matched to which arguments when calling pmap on a data.frame or named list.

The default behaviour, which is useful and intuititive in most contexts is to match column names to argument names, as in

check_row_deidentified <- function(encntr_key, clinical_event_key, note_value, ...) { 
  # Do stuff
  tibble::tibble(encntr_key, clinical_event_key, note_value)
}

notes_1 <- tibble::tibble(
  encntr_key = c(1,2,3),
  clinical_event_key = c(1,2,3),
  note_value = c("foo", "bar", "baz")
  ) 


out <- notes_1 %>%
 purrr::pmap_dfr(check_row_deidentified)

But I'm wondering if there's a way to give an input with different column names, and specify how pmap should treat those.

As in:

check_row_deidentified <- function(encntr_key, clinical_event_key, note_value, ...) { 
  # Do stuff
}

notes_1 <- tibble::tibble(
  key_enc = c(1,2,3),
  key_clinical = c(1,2,3),
  free_text = c("foo", "bar", "baz")
  ) 


out <- notes_1 %>%
 purrr::pmap_dfr(check_row_deidentified, encntr_key = key_enc, clinical_event_key = key_clinical, note_value = note_value)

I guess an obvious choice would be to just rename those columns before I call pmap, as in

# Given as arg
to_rename <- rlang::exprs(
  note_value = free_text,
  encntr_key = key_enc,
  clinical_event_key = key_clinical
)

notes_1 %>%
  dplyr::rename(!!!to_rename) %>%
  purrr::pmap_dfr(check_row_deidentified)

But I'm not sure if something less cludgey exists

Upvotes: 1

Views: 204

Answers (1)

Matthew Strasiotto
Matthew Strasiotto

Reputation: 369

I decided that my "cludgey" workaround is good enough for this excercise:

I guess an obvious choice would be to just rename those columns before I call pmap, as in

# Given as arg
to_rename <- rlang::exprs(
  note_value = free_text,
  encntr_key = key_enc,
  clinical_event_key = key_clinical
)

notes_1 %>%
  dplyr::rename(!!!to_rename) %>%
  purrr::pmap_dfr(check_row_deidentified)

But I'm not sure if something less cludgey exists

Upvotes: 1

Related Questions