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]>
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.
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.
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.
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") ) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With