R data frame: columns by count [duplicate]

all,

I have a data frame with thousands of elements along the lines of

rawdata <- data.frame("Pop" = c("A","B","B","C","C") , "ID" = c(1,2,2,1,1))

Pop ID
A 1
B 2
B 2
C 1
C 1

and am trying to create an output like

ID A B C
1 1 0 2
2 0 2 0

But I'm struggling to figure how to do this. Would anyone happen to know a way?

Thank you for your time.


Solution 1:

We can create counts of occurrences following a group_by to summarise procedure, or use the dplyr function count directly as a shortcut. To then transform the data into a wide shape, pivot_wider can be used. Here the values are taken from the column n, the default column name that is the reuslt of count. Lastly, NA values are to be transformed into zeros.

library(tidyverse)
rawdata %>%
  count(Pop, ID) %>%
  pivot_wider(names_from = Pop, values_from = n, values_fill = 0)
     ID     A     B     C
  <dbl> <int> <int> <int>
1     1     1     0     2
2     2     0     2     0