Home > other >  Mapping over a nested column and expanding a data frame into a new nested column
Mapping over a nested column and expanding a data frame into a new nested column

Time:04-23

I am trying to create a new nested column using the data from the min and max values of another nested column.

If I nest the IRIS data by Species and want to create a new nested data frame by the min and max of the Petal.Length for each Species how would I do it?

My code so far, create a function to create a new data.frame or expand.grid, then apply it using mutate(...map(...

Code/Data:

func = function(input){
  data.frame(
    min_to_max = seq(
      from = min(.x$Petal.Length),
      to = max(.x$Petal.Length),
      by = 1
    )
  )
}

iris %>% 
  group_by(Species) %>% 
  nest() %>% 
  mutate(
    expandDF = map(data, ~ func(.x))
  )

CodePudding user response:

The function should have match the argument name used i.e. input and not .x

func <- function(input){
     data.frame(
       min_to_max = seq(
         from = min(input$Petal.Length),
         to = max(input$Petal.Length),
         by = 1
       )
     )
   }

-testing

iris %>% 
  group_by(Species) %>% 
  nest() %>% 
  mutate(
    expandDF = map(data, ~ func(.x))
  ) %>% ungroup

-output

# A tibble: 3 × 3
  Species    data              expandDF    
  <fct>      <list>            <list>      
1 setosa     <tibble [50 × 4]> <df [1 × 1]>
2 versicolor <tibble [50 × 4]> <df [3 × 1]>
3 virginica  <tibble [50 × 4]> <df [3 × 1]>

We could also do this without using map i.e with nest_by

iris %>%
  nest_by(Species) %>%
  mutate(expandDF = list(data.frame(min_to_max = 
   seq(from = min(data$Petal.Length), to = max(data$Petal.Length))))) %>%
 ungroup
# A tibble: 3 × 3
  Species                  data expandDF    
  <fct>      <list<tibble[,4]>> <list>      
1 setosa               [50 × 4] <df [1 × 1]>
2 versicolor           [50 × 4] <df [3 × 1]>
3 virginica            [50 × 4] <df [3 × 1]>
  • Related