I have a very large data frame with other variables in it, but I would like to collapse this data frame by id and create new columns with the frequency counts of the obj code.
This is part of my dataframe.
id <- c("Adam", "Adam", "Adam", "Adam", "Adam", "Adam", "John", "John", "John", "Kim")
obj <- c("21", "21", "22", "23", "24", "25", "25", "27", "28", "28")
df <- data.frame(id, obj)
And this is my desired output:
id2 <- c("Adam", "John", "Kim")
obj.21 <- c(2,0,0)
obj.22 <- c(1,0,0)
obj.23 <- c(1,0,0)
obj.24 <- c(1,0,0)
obj.25 <- c(1,1,0)
obj.27 <- c(0,1,0)
obj.28 <- c(0,1,1)
output <- data.frame(id2, obj.21, obj.22,obj.23,obj.24,obj.25,obj.27,obj.28)
output
I want to count the number of times that obj code (obj) appears for each person (id). Each obj code should have their own column with the counts.