1
votes

I understand how to use split, lapply and the combine the list outputs back together using base R. I'm trying to understand the purrr way to do this. I can do it with base R and even with purrr* but am guessing since I seem to be duplciating the order variable that I'm doing it wrong. It feels clunky so I don't think I get it.

What is the tidyverse approach to using info from data subsets to create a nested output column?

Base R approach to make nested column in a data frame

library(tidyverse)
set.seed(10)

dat2 <- dat1 <- data_frame(
    v1 = LETTERS[c(1, 1, 1, 1, 2, 2, 2, 2)],
    v2 = rep(1:4, 2),
    from = c(1, 3, 2, 1, 3, 5, 2, 1),
    to = c(1, 3, 2, 1, 3, 5, 2, 1) + sample(1:3, 8, TRUE)
)

dat1 <- split(dat1, dat1[c('v1', 'v2')]) %>%
    lapply(function(x){ 
        x$order <- list(seq(x$from, x$to))
        x
    }) %>%
    {do.call(rbind, .)}

dat1
unnest(dat1)

My purrr approach (what is the right way?)

dat2 %>%
    group_by(v1, v2) %>%
    nest() %>%
    mutate(order = purrr::map(data, ~ with(., seq(from, to))))  %>%
    select(-data) 

Desired output

  v1       v2  from    to order    
* <chr> <int> <dbl> <dbl> <list>   
1 A         1     1     3 <int [3]>
2 B         1     3     4 <int [2]>
3 A         2     3     4 <int [2]>
4 B         2     5     6 <int [2]>
5 A         3     2     4 <int [3]>
6 B         3     2     3 <int [2]>
7 A         4     1     4 <int [4]>
8 B         4     1     2 <int [2]>
2
Am I misunderstanding something, or is the ordering of your data example a little messed up? The elements of outs end up in a different order than how they appears in the data frame...joran
@joran no I'm missing something...let me fix, this was a toy example and I missed thisTyler Rinker
Ok, in the meantime, I'll share my initial thought, which may not be relevant once you fix things: mutate(dat2,order = map2(.x = from,.y = to,.f = seq)).joran
@joran in this particular case that's it. Can you post as answer?Tyler Rinker

2 Answers

2
votes

In this particular case it seems you're looking for:

mutate(dat2,order = map2(.x = from,.y = to,.f = seq))
1
votes

Using the new, experimental, rap package:

remotes::install_github("romainfrancois/rap")

library(rap)

dat2 %>% 
  rap(order = ~ seq(from, to))