Skip to content

Instantly share code, notes, and snippets.

@akgold
Last active January 29, 2020 14:58
Show Gist options
  • Save akgold/a480cb150de84ef9016fa8f3ef6a59ce to your computer and use it in GitHub Desktop.
Save akgold/a480cb150de84ef9016fa8f3ef6a59ce to your computer and use it in GitHub Desktop.
map_df Explained
---
title: "map_df Example"
author: "Alex Gold"
date: "1/29/2020"
output: html_document
---
```{r setup, include=FALSE}
library(tidyverse)
```
From the docs for map_df, the function signature of map_df is `map_df(.x, .f, ...)`. `.f` can be a function or character, if a character, it's used to create an extractor function. If it's a character, anything in `...` gets passed on to the extractor function.
By providing `my_data %>% map_df("attributes", "features")`, `.f = "attributes"`, and `... = "features"`.
Guessing at what your data looks like since you said, `my_data[["features"]][["attributes]]` works...
```{r}
(my_data <- list(
features = list(
attributes = tibble(
dat = 1:3,
dat2 = 4:6
),
other_thing = 1:4
),
other_thing2 = 1:3
)
)
```
So the df I want is in
```{r}
my_data[["features"]][["attributes"]]
```
This doesn't work
```{r}
my_data %>% map_df("features", "attributes")
```
because `map_df` is mapping through the first nested layer, so it's _inside_ the features list when it's trying to do the first extraction. In other words, the function is first looking at `my_data$features` and trying to extract `"features"`, but there isn't an object named `"features"` inside `my_data$features`.
There is an object named `"attributes"`, so this works
```{r}
my_data %>% map_df("attributes")
```
This does too
```{r}
my_data %>% map_df("attributes", "features")
```
Because the argument `"features"` is being passed as an additional argument to the extractor function and not actually doing anything.
It's kinda dangerous to do it this way -- `map` is going across all of the sub-lists in `my_data`, so it's just lucky that there are no other `features` or `attributes` for it to give back things you _don't_ want.
In this case, I don't need a `map` function at all because I'm not actually iterating over anything. The most succint way to do this in a pipe is
```{r}
my_data %>% magrittr::extract2(c("features", "attributes"))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment