【问题标题】:r Group by and countr 分组和计数
【发布时间】:2017-02-28 08:36:21
【问题描述】:

我正在处理如下数据集

      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

代表

1st Row , 2nd 列 (Representation) 中的值为 3,因为 Red 基于 ID 和 Date 的唯一组合表示了 3 次。例如,1st 和 2nd 行是相同的,Id(10) 和 Date(2008-11-17) 所以这个组合表示一次(1(10,2008-11-17))。第 4th 和 7th 行是相同的 Id(10) 和 Date(2010-01-26) 组合,所以这个唯一的组合,表示一次 (1(10, 2010-01-26)) 。第 8th、9th、12th 是 Id(29) 和 Date(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

计数

第 1st 行,第 3rd 列(计数)中的值是 7,因为 ID 102008-11-17 上两次提到了 Red(2 10, 2008-11-17),ID 102010-01-26 (2 10, 2010-01-26) 上再次提到 Red 两次,并由ID 29 2007-07-31 2 29,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)  

【问题讨论】:

  • 两张表? cbind(data.frame(table(df$Color)), Rep = colSums(!!table(interaction(df$Id, df$Date), df$Color)))

标签: r group-by dplyr plyr reshape2


【解决方案1】:

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(四分位间距)中的值的范围(最大值-最小值)?
  • 分别使用range()IQR() 函数。
  • :) 这两个都没有用,所以我尝试了这个,min(n())max(n()),我没有得到正确的值,
【解决方案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

【讨论】:

    【解决方案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
    

    更新

    第二个问题,我们可以试试

    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
    

    【讨论】:

    • @HeatherKeturah 你是说setDT(df)[, .(Representation = uniqueN(paste(Id, Date)), Count = .N) , by = Color][, c("Min", "Max", "Iqr") := .(min(Count), max(Count), IQR(Count))][]
    • @HeatherKeturah 或者如果你想使用range函数,那么setDT(df)[, .(Representation = uniqueN(paste(Id, Date)), Count = .N) , by = Color][, c("Min", "Max", "Iqr") := c(as.list(range(Count)), IQR(Count))][]
    • @HeatherKeturah 我不确定您是如何获得这些值的。根据此处显示的输出,对于红色和绿色,没有关于 1、1 或 1、2 的信息
    • @HeatherKeturah 如果您需要 IQR,那么 rowIQRs(m1) 将是另一列
    • 那是完美的。我从来没有对 matrixStats 包感到陌生,我正在查看这个包的文档,我发现了很多有趣的东西。非常感谢,我希望我能给 100 分 :)
    猜你喜欢
    • 2021-11-10
    • 2018-08-02
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    相关资源
    最近更新 更多