Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format dplyr do() output by group into data.frame

Tags:

r

dplyr

I use dplyr to interpolate different length curves to identical length curves with the do() function.

The last step however, to properly format the results eludes me. Currently I get a data.frame with the following setup: a single row per group, with for each variable a list with the outcomes.

    library(dplyr)
    library(tidyr)

    df = data.frame(id = c(rep('a', 6), rep('b', 8)),
                    time = c(1:6, 1:8),
                    val = c(0.1, 0.2 ,0.4,0.6,0.75,1,
                            0.1, 0.25, 0.45, 0.6, 0.8, 0.9, 0.95, 1),
                    stringsAsFactors = FALSE) %>%
      group_by(id) %>% 
      mutate(total = n(),
             perc = time / total) %>%
      do(elapsed_perc= 1:5 / 5,
         duration_prog=approx(x=.$perc, y=.$val, xout = 1:5 / 5,)$y)

Instead, I would like to obtain as many rows per group as there are observations. So in the case below, I would like 5 rows for both group 'a' as group 'b'.

A solution would be to recreate the data.frame (which I can of course easily make dynamic):

df.new = data.frame(id = rep(c('a', 'b'), each = 5),
           elapsed_perc = unlist(df$elapsed_perc),
           duration_prog = unlist(df$duration_prog))

However, I would like to do this without having to create a new data frame. Do any of you know how to do that?

like image 774
user1193435 Avatar asked May 13 '16 11:05

user1193435


1 Answers

We can use unnest

library(tidyr)
unnest(df, elapsed_perc, duration_prog)
#      id elapsed_perc duration_prog
#     <chr>        <dbl>         <dbl>
#1      a          0.2          0.12
#2      a          0.4          0.28
#3      a          0.6          0.52
#4      a          0.8          0.72
#5      a          1.0          1.00
#6      b          0.2          0.19
#7      b          0.4          0.48
#8      b          0.6          0.76
#9      b          0.8          0.92
#10     b          1.0          1.00
like image 112
akrun Avatar answered Oct 01 '22 12:10

akrun