0

enter image description here

I need to know how many different items are produced by each company (ID). The result should be this:

  • Company 1 = 4 (A,B,C,D)
  • Company 2 = 1 (B)
  • Company 3 = 2 (A,B)
  • Company 4 = 2 (A,B)

Which code should I use to get there? I guess it should be something that allows me to count the unique values for each ID.

Thanks

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
FTc
  • 27
  • 4

1 Answers1

0

I think you can use split()+unique()+nrow() to make it, i.e.,

cnt <- sapply(split(u<-unique(df),u$ID),nrow)

or just

cnt <- table(unique(df)$ID) # from comments of @H1

such that

> cnt
1 2 3 4 
4 1 2 2

DATA

df <- structure(list(ID = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4), 
    Items = structure(c(1L, 2L, 3L, 4L, 2L, 2L, 2L, 1L, 1L, 2L, 
    1L, 1L, 2L), .Label = c("A", "B", "C", "D"), class = "factor")), class = "data.frame", row.names = c(NA, 
-13L))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81