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?
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
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