Dplyr :: nested data frame selection

I have the following example data frame:

library(tibble)
library(tidyverse)
df <- tibble(A = 1, B = 1)
df2 <- tibble(C = 2:4, D = 4:6)
df <- df %>%
        nest(B) %>%
        mutate(data = map(data, ~df2))

      

It is a nested dataframe 3x2

( df2

) in a dataframe 1x2

( df

). Is there a way to combine purrr::map

and dplyr::select

to select only column C

in a nested dataframe? I hope to avoid unnest

.
The result should be:

      A             data
  <dbl>           <list>
1     1 <tibble [3 x 1]>

      

+3


source to share


1 answer


After you have created a nested dataset, you can use select

the map

in the "data" column in the same call mutate

.



df %>%
    nest(B) %>%
    mutate(data = map(data, ~df2),
           data = map(data, ~select(.x, "C") ) )

      

+3


source







All Articles