0

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.

mellymoo
  • 77
  • 6

2 Answers2

0

One option would be table

table(df)
#      obj
#id     21 22 23 24 25 27 28
#  Adam  2  1  1  1  1  0  0
#  John  0  0  0  0  1  1  1
#  Kim   0  0  0  0  0  0  1

Or using tidyverse

library(tidyverse)
df %>%
   count(id, obj) %>%
   mutate(obj = str_c("obj", obj)) %>%
   spread(obj, n, fill = 0)
# A tibble: 3 x 8
#  id    obj21 obj22 obj23 obj24 obj25 obj27 obj28
#  <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Adam      2     1     1     1     1     0     0
#2 John      0     0     0     0     1     1     1
#3 Kim       0     0     0     0     0     0     1
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Using data.table:

library(data.table)
dcast(df, id ~ paste0("obj", obj), value.var = "obj", fun.aggregate = length)

    id obj21 obj22 obj23 obj24 obj25 obj27 obj28
1 Adam     2     1     1     1     1     0     0
2 John     0     0     0     0     1     1     1
3  Kim     0     0     0     0     0     0     1
s_baldur
  • 29,441
  • 4
  • 36
  • 69