从 R 中的列中删除双精度值

Removing double values from a column in R

我需要知道每个公司(ID)生产多少种不同的物品。 结果应该是这样的:

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

我应该使用哪个代码到达那里?我想它应该能让我计算每个 ID 的唯一值。

谢谢

我想你可以用split()+unique()+nrow()来实现,即

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

或者只是

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

这样

> cnt
1 2 3 4 
4 1 2 2

数据

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))