I'd like to create a new list with duplicate entries based upon an existing list in R. I'm trying to use tidyverse as much as possible, so dplyr would be preferred.
Say I have a list of times where sales occured:
df <- data.frame(time = c(0,1,2,3,4,5), sales = c(1,1,2,1,1,3))
> df
time sales
1 0 1
2 1 1
3 2 2
4 3 1
5 4 1
6 5 3
And I'd like instead to have a list with an entry for each sale:
ans <- data.frame(salesTime = c(0,1,2,2,3,4,5,5,5))
> ans
salesTime
1 0
2 1
3 2
4 2
5 3
6 4
7 5
8 5
9 5
I found an interesting example using dplyr here: Create duplicate rows based on conditions in R
But this will only allow me to create one new row when sales == n, and not create n new rows when sales == n.
Any help would be greatly appreciated.
A nice tidyr
function for this is uncount()
:
df %>%
uncount(sales) %>%
rename(salesTime = time)
salesTime
1 0
2 1
3 2
3.1 2
4 3
5 4
6 5
6.1 5
6.2 5
data.frame(salesTime = rep(df$time, df$sales))
# salesTime
#1 0
#2 1
#3 2
#4 2
#5 3
#6 4
#7 5
#8 5
#9 5
If you like dplyr and pipes you can go for:
df %>% {data.frame(salesTime = rep(.$time, .$sales))}
df %>% rowwise %>% mutate(time=list(rep(time,sales))) %>% unnest
## A tibble: 9 x 2
# sales time
# <dbl> <dbl>
#1 1 0
#2 1 1
#3 2 2
#4 2 2
#5 1 3
#6 1 4
#7 3 5
#8 3 5
#9 3 5
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