-1

I want to transform a df from a "counting" approach (number of cases) to a "individual observations" approach.

Example:

df <- dplyr::tibble(
  city = c("a", "a", "b", "b", "c", "c"), 
  sex = c(1,0,1,0,1,0),
  age = c(1,2,1,2,1,2),
  cases = c(2, 3, 1, 1, 1, 1))

Expected result

df <- dplyr::tibble(
  city = c("a","a","a","a","a", "b", "b", "c", "c"), 
  sex = c(1,1,0,0,0,1,0,1,0),
  age = c(1,1,2,2,2,1,2,1,2))
user438383
  • 5,716
  • 8
  • 28
  • 43
Amc
  • 131
  • 8

1 Answers1

0

uncount() from tidyr can do that for you.

df |> tidyr::uncount(cases)
Andrea M
  • 2,314
  • 1
  • 9
  • 27