按组计数

3
我正在处理一个数据集,其格式如下:
      Id     Date           Color
      10     2008-11-17     Red
      10     2008-11-17     Red
      10     2008-11-17     Blue
      10     2010-01-26     Red
      10     2010-01-26     Green
      10     2010-01-26     Green
      10     2010-01-26     Red
      29     2007-07-31     Red
      29     2007-07-31     Red
      29     2007-07-31     Blue
      29     2007-07-31     Green
      29     2007-07-31     Red

我的目标是创建一个像这样的数据集

     Color      Representation      Count            Min   Max
     Red        1 + 1 + 1  = 3      2 + 2 + 3 = 7    2     3
     Blue       1 + 1      = 2      1 + 1            1     1
     Green      1 +  1     = 2      2 + 1            1     2

数据表现

在第一行第二列(数据表现)中的值为3,因为根据ID和日期的唯一组合,红色被表示了三次。例如,第1行和第2行是相同的,ID(10)和日期(2008-11-17),所以这个组合只被表示一次(1(10, 2008-11-17))。第4行和第7行是相同的,ID(10)和日期(2010-01-26)的组合,所以这个唯一的组合被表示一次(1(10, 2010-01-26))。第8、9和12行是ID(29)和日期(2007-07-31)的相同组合,同样只被表示一次(1(29, 2007-07-31))。因此,在第1行第2列中的值为3。

1(10, 2008-11-17) + 1(10, 2010-10-26) + 1(29, 2007-07-31) = 3

计数

在第一行第三列(计数)中的值为7,因为红色在ID为10的记录中在2008-11-17出现了两次(210, 2008-11-17),在2010-01-26出现了两次(210, 2010-01-26),在ID为29的记录中在2007-07-31出现了三次(329,2007-07-31)。

2(10, 2008-11-17) + 2(10, 2010-10-26) + 3(29, 2007-07-31)

非常感谢您对完成这个唯一频率/计数表的帮助。

数据集

Id   = c(10,10,10,10,10,10,10,29,29,29,29,29) 
Date = c("2008-11-17", "2008-11-17", "2008-11-17","2010-01-26","2010-01-26","2010-01-26","2010-01-26",
         "2007-07-31","2007-07-31","2007-07-31","2007-07-31","2007-07-31") 
Color = c("Red", "Red", "Blue", "Red", "Green", "Green", "Red", "Red", "Red", "Blue", "Green", "Red") 
df = data.frame(Id, Date, Color)  

2
两个表格?cbind(data.frame(table(df$Color)), Rep = colSums(!!table(interaction(df$Id, df$Date), df$Color))) - rawr
3个回答

4
使用 dplyr
library(dplyr)
dat %>% group_by(Color) %>%
    summarize(Representation = n_distinct(Id, Date), Count = n())
# # A tibble: 3 × 3
#    Color Representation Count
#   <fctr>          <int> <int>
# 1   Blue              2     2
# 2  Green              2     3
# 3    Red              3     7

那很完美,我如何捕获列“Count”中值的范围(最大值-最小值),以及IQR(四分位距)? - Heather Keturah
通过使用range()IQR()函数。 - Gregor Thomas
这两个不起作用,所以我尝试了 min(n())max(n()),但是我没有得到正确的值。 - Heather Keturah

3
另一种选择是data.table
library(data.table)
setDT(df)[, .(Representation = uniqueN(paste(Id, Date)), Count = .N) , by = Color]
#     Color Representation Count
#1:   Red              3     7
#2:  Blue              2     2
#3: Green              2     3

Update

For the second question, we can try

library(matrixStats)
m1 <- sapply(split(df[["Color"]], list(df$Id, df$Date), drop = TRUE),  function(x) table(x))
v1 <- (NA^!m1) * m1
df1 <- data.frame(Color = row.names(m1), Representation = rowSums(m1!=0), 
   Count = rowSums(m1), Min = rowMins(v1, na.rm=TRUE),
    Max = rowMaxs(v1, na.rm=TRUE))
row.names(df1) <- NULL
df1
#   Color Representation Count Min Max
#1  Blue              2     2   1   1
#2 Green              2     3   1   2
#3   Red              3     7   2   3

1
@HeatherKeturah 你是不是想说 setDT(df)[, .(Representation = uniqueN(paste(Id, Date)), Count = .N) , by = Color][, c("Min", "Max", "Iqr") := .(min(Count), max(Count), IQR(Count))][] - akrun
1
如果您想使用range函数,则可以使用以下代码:setDT(df)[, .(Representation = uniqueN(paste(Id, Date)), Count = .N) , by = Color][, c("Min", "Max", "Iqr") := c(as.list(range(Count)), IQR(Count))][]。@HeatherKeturah - akrun
2
@HeatherKeturah 我不确定你是如何得到那些值的。根据这里显示的输出,关于红色和绿色的1、1或1、2没有那些信息。 - akrun
1
@HeatherKeturah 如果您需要IQRs,则rowIQRs(m1)将是另一列。 - akrun
1
那太完美了。我以前不知道有matrixStats包,现在我正在查看这个包的文档,发现了很多有趣的东西。非常感谢,真希望我能给100分 :) - Heather Keturah
显示剩余3条评论

2
您可以使用aggregate()函数:
# Make a new column for the Date-Id joined (what you want to base the counts on
df$DateId <- paste(df$Date, df$Id)

# Get the representation values
Representation <- aggregate(DateId ~ Color, data=df,FUN=function(x){length(unique(x))})
Representation
#>   Color DateId
#> 1  Blue      2
#> 2 Green      2
#> 3   Red      3

# Get the Count values
Count <- aggregate(DateId ~ Color, data=df,FUN=length)
Count
#>   Color DateId
#> 1  Blue      2
#> 2 Green      3
#> 3   Red      7

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接