The opposite of count()

R
TIL
reshaping data
Author

Sharon Howard

Published

1 Mar 2023

Problem: I had a dataset in which values had been summarised, but for a particular analysis I wanted to dis-aggregate them.

Solution: tidyr::uncount(). Does the opposite of dplyr::count().

library(tidyverse)

df <-
tribble(~"name", ~"count",
        "John", 1,
        "Jane", 4,
        "James", 2)

df
# A tibble: 3 × 2
  name  count
  <chr> <dbl>
1 John      1
2 Jane      4
3 James     2
df |>
  uncount(count)
# A tibble: 7 × 1
  name 
  <chr>
1 John 
2 Jane 
3 Jane 
4 Jane 
5 Jane 
6 James
7 James