Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Create duplicate rows based on a variable (dplyr preferred) [duplicate]

Tags:

r

dplyr

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.

like image 872
colton Avatar asked Sep 25 '18 12:09

colton


3 Answers

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
like image 173
tmfmnk Avatar answered Nov 12 '22 15:11

tmfmnk


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))}
like image 28
Andre Elrico Avatar answered Nov 12 '22 16:11

Andre Elrico


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
like image 1
Nicolas2 Avatar answered Nov 12 '22 15:11

Nicolas2