Creating multiple Columns based on frequency in main column by ID

Hey all,

I have a dataset with ID's and the types of fitness classes they attended over the course of a year. I was able to aggregate and clean the data but I'm getting stuck on how to create a bunch of columns (for each fitness class) and the frequency of which they attended.

Here's a snip of what my data looks like:

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

And I'd want it to tranform into:
ID spin yoga bootcamp aerobics
101 50 25 0 0
102 50 25 20 10
103 0 50 0 10

Thank you!

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 :slightly_smiling_face:

1 Like

Legend! Thank you sir!

Sure thing @Sportsci - Just recalled, that you can actually set the values_fill parameter, so I have just updated my answer :+1:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.