Imagine I have the following dataset:
tid = c(1,1,2,2,2,3,3,3,3)
count = c(2,2,3,3,3,4,4,4,4)
item = c(1,2,7,9,1,12,3,2,9)
name = c("rice","beans","meat","lettuce","rice","soda","water","beans","lettuce")
dat = data.frame(tid,count,item,name)
These are three transaction with 2, 3 and 4 items respectively. For each transaction I would like to drop at random one item.
What I would like to do is create two data frames: one with the dropped item and the other one with the kept items.
This is what I've done:
dat2 <- dat %>%
group_nest(tid,count)dat2$deleted <- dat2$count %>%
map_int(sample,size=1)
Now I would like to create two new columns in "dat2": one that contains the tibble with the dropped item and one that contains the tibble with the kept items.
I hope I was clear enough