Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr::select of nested data frame

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's a nested 3x2 data frame (df2) in a 1x2 data frame (df). Is there a way to combine purrr::map and dplyr::select to select only column C in the nested data frame? I'm hoping to avoid unnest. The outcome should be:

      A             data
  <dbl>           <list>
1     1 <tibble [3 x 1]>
like image 874
CPak Avatar asked Jun 30 '17 18:06

CPak


People also ask

What does Nest () do in R?

Nesting creates a list-column of data frames; unnesting flattens it back out into regular columns. Nesting is implicitly a summarising operation: you get one row for each group defined by the non-nested columns. This is useful in conjunction with other summaries that work with whole datasets, most notably models.

How do I Unnest a data frame in R?

The tidyr package in R is used to “tidy” up the data. The unnest() method in the package can be used to convert the data frame into an unnested object by specifying the input data and its corresponding columns to use in unnesting. The output is produced in the form of a tibble in R.

How do I convert a nested list to a Dataframe in R?

frame() converts the nested list to an R DataFrame by taking do. call() function as a parameter. So each list inside a nested list will be a column in a DataFrame. So the column names in the DataFrame will be nested list names.


1 Answers

Once you've made the nested dataset that you have, you can use select in map on the "data" column in the same mutate call.

df %>%
    nest(B) %>%
    mutate(data = map(data, ~df2),
           data = map(data, ~select(.x, "C") ) )
like image 154
aosmith Avatar answered Sep 29 '22 08:09

aosmith