0

I have a data frame

catch    yr     mesh
253     2013    0       
4947    2013    1       
3656    2013    1.25        
3259    2013    1.5     
2902    2013    1.75        
4605    2013    2       
764     2013    2.25        
256     2013    2.5

I want to insert rows that repeat the values in "yr" and "mesh" by the corresponding row value in the "catch" column.

So, I would like 253 rows that have a value of 253 for catch, 2013 for yr, & 0 for mesh...4947 rows w/ a value of 4947 for catch, 2013 for year, 1 for mesh, etc.

vermicellion
  • 338
  • 2
  • 14

1 Answers1

1

tidyr::uncount() does precisely this.

library(tidyr)

uncount(catch_data, catch, .remove = FALSE)

Output:

# A tibble: 20,642 × 3
   catch    yr  mesh
   <dbl> <dbl> <dbl>
 1   253  2013     0
 2   253  2013     0
 3   253  2013     0
 4   253  2013     0
 5   253  2013     0
 6   253  2013     0
 7   253  2013     0
 8   253  2013     0
 9   253  2013     0
10   253  2013     0
# … with 20,632 more rows
zephryl
  • 14,633
  • 3
  • 11
  • 30