library("tidyverse")
d_long <- tribble(
~ID, ~Class, ~n,
101, "spin", 50,
101, "yoga", 25,
102, "spin", 50,
102, "yoga", 25,
102, "bootcamp", 20,
102, "aerobics", 10,
103, "yoga", 50,
103, "aerobics", 10
)
d_wide <- d %>%
pivot_wider(id_cols = ID,
names_from = Class,
values_from = n,
values_fill = 0)
Yielding:
> d_long
# A tibble: 8 x 3
ID Class n
<dbl> <chr> <dbl>
1 101 spin 50
2 101 yoga 25
3 102 spin 50
4 102 yoga 25
5 102 bootcamp 20
6 102 aerobics 10
7 103 yoga 50
8 103 aerobics 10
> d_wide
# A tibble: 3 x 5
ID spin yoga bootcamp aerobics
<dbl> <dbl> <dbl> <dbl> <dbl>
1 101 50 25 0 0
2 102 50 25 20 10
3 103 0 50 0 10
Hope it helps 