【问题标题】:R count distinct elements based on two columns by groupR根据两列按组计算不同的元素
【发布时间】:2018-08-15 23:53:11
【问题描述】:

我基本上有这个数据,但更大:

我想计算 (customer_id, account_id) 的不同组合的数量 - 即基于两列的不同或唯一值,但针对每个 start_date。我在任何地方都找不到解决方案。结果应该是添加到我的 data.table 中的另一列,应该如下所示:

也就是说,对于每个 start_date,它会根据 customer_id 和 account_id 计算不同值的数量。

例如,对于 start_date 等于 2.2.2018,我在 (customer_id,account_id) 中有不同的组合是 (4,22) (5,38) 和 (6,13),所以我希望计数等于3 因为我有 3 种不同的组合。我还需要使用 customer_id 和 account_id 列中的字符值的解决方案。

复制数据的代码:

customer_id <- c(1,1,1,2,3,3,4,5,5,6)
account_id <- c(11,11,11,11,55,88,22,38,38,13)
start_date <- c(rep(as.Date("2017-01-01","%Y-%m-%d"),each=6),rep(as.Date("2018-02-02","%Y-%m-%d"),each=4))

data <- data.table(customer_id,account_id,start_date)

【问题讨论】:

  • 请不要发数据图片,使用head(dput(...))发布实际数据。
  • R 术语将是 “添加计数列,显示每个 start_date 的(customer_id,account_id)不同组合的计数”
  • 重复,至少对于 data.table 方法:Count number of unique rows based on two columns, by group,使用DT[, count:=uniqueN(.SD) ...

标签: r


【解决方案1】:

另一个dplyr 选项:

library(dplyr)
customer_id <- c(1,1,1,2,3,3,4,5,5,6)
account_id <- c(11,11,11,11,55,88,22,38,38,13)
start_date <- c(rep(as.Date("2017-01-01","%Y-%m-%d"),each=6),rep(as.Date("2018-02- 
02","%Y-%m-%d"),each=4))

data <- data.frame(customer_id,account_id,start_date)

data %>%
  group_by(start_date)%>%
  mutate(distinct_values = n_distinct(customer_id, account_id)) %>%
  ungroup()

【讨论】:

    【解决方案2】:

    dplyr 选项

    customer_id <- c(1,1,1,2,3,3,4,5,5,6)
    account_id <- c(11,11,11,11,55,88,22,38,38,13)
    start_date <- c(rep(as.Date("2017-01-01","%Y-%m-%d"),each=6),rep(as.Date("2018-02- 
    02","%Y-%m-%d"),each=4))
    
    data <- data.frame(customer_id,account_id,start_date)
    
    data %>% 
      group_by(start_date, customer_id, account_id) %>% 
      summarise(Total = 1) %>% 
      group_by(start_date) %>% 
      summarise(Count =n())
    

    【讨论】:

      【解决方案3】:

      这是一个data.table 选项

      data[, N := uniqueN(paste0(customer_id, account_id, "_")), by = start_date]
      #    customer_id account_id start_date N
      # 1:           1         11 2017-01-01 4
      # 2:           1         11 2017-01-01 4
      # 3:           1         11 2017-01-01 4
      # 4:           2         11 2017-01-01 4
      # 5:           3         55 2017-01-01 4
      # 6:           3         88 2017-01-01 4
      # 7:           4         22 2018-02-02 3
      # 8:           5         38 2018-02-02 3
      # 9:           5         38 2018-02-02 3
      #10:           6         13 2018-02-02 3
      

      或者

      data[, N := uniqueN(.SD, by = c("customer_id", "account_id")), by = start_date]
      

      【讨论】:

      • 非常感谢,这很好用!我能问一下为什么paste0函数中需要“_”吗?
      • @doremi 在第一个解决方案中,我们连接来自customer_idaccount_id 的条目;用"_" 分隔条目确保我们可以区分"1_11""11_1"。如果没有"_",两者都会变成"111"。我希望这有助于解决问题:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2021-01-14
      相关资源
      最近更新 更多