0

I´m trying to fit several models and wanted to use a tibble to organize all the data, however I´m stuck at trying to split each dataset

here is the example:

    library(tidyverse)
    library(tidymodels)

    my_data <-crossing(labels = c("A", "B", "C", "D"),
        first_var = seq(1,100, 200),
        second_var = rnorm(100, 0.25, 0.02))

    my_data_nested <- my_data %>% nest(vars = c(first_var, second_var))

  # A tibble: 4 x 2 
  labels vars              
  <chr>  <list>            
1 A      <tibble [100 x 2]>
2 B      <tibble [100 x 2]>
3 C      <tibble [100 x 2]>
4 D      <tibble [100 x 2]>

I´d like to add a column with the "initial_split" from the rsample package

when I try:

    my_data_i_split <- my_data_nested %>% map(vars  ~ initial_split(.x))
   Error: Can't convert a two-sided formula to a function
   Run `rlang::last_error()` to see where the error occurred.    

I get this error that goes well above my r knowledge.

Aurèle
  • 12,545
  • 1
  • 31
  • 49
edperalt
  • 23
  • 5
  • 2
    Maybe this is the syntax you're looking for: `my_data_nested %>% mutate(vars = map(vars, initial_split))`. Where `my_data_nested` is passed to `mutate` instead of `map` directly, and `map` takes two arguments: first the list to iterate on (as column `vars`), second the function to apply (`initial_split`, or `~ initial_split(.x)` for an anonymous function syntax closer to your attempt) – Aurèle Dec 18 '20 at 15:40
  • See https://tidyr.tidyverse.org/articles/nest.html#nested-data-and-models for additional examples – Aurèle Dec 18 '20 at 15:41
  • Excellent, is just what I needed, many thanks!! – edperalt Dec 18 '20 at 15:56

0 Answers0