r - 每组唯一值的累积计数

标签 r count unique cumulative-frequency

我有一个带有姓名和一些资格状态日期的 df。我想根据时间创建一个指示一个人有多少唯一 elig_end_dates。这是我的 df:

 names date_of_claim elig_end_date
1    tom    2010-01-01    2010-07-01
2    tom    2010-05-04    2010-07-01
3    tom    2010-06-01    2014-01-01
4    tom    2010-10-10    2014-01-01
5   mary    2010-03-01    2014-06-14
6   mary    2010-05-01    2014-06-14
7   mary    2010-08-01    2014-06-14
8   mary    2010-11-01    2014-06-14
9   mary    2011-01-01    2014-06-14
10  john    2010-03-27    2011-03-01
11  john    2010-07-01    2011-03-01
12  john    2010-11-01    2011-03-01
13  john    2011-02-01    2011-03-01

这是我想要的输出:
 names date_of_claim elig_end_date obs
1    tom    2010-01-01    2010-07-01   1
2    tom    2010-05-04    2010-07-01   1
3    tom    2010-06-01    2014-01-01   2
4    tom    2010-10-10    2014-01-01   2
5   mary    2010-03-01    2014-06-14   1
6   mary    2010-05-01    2014-06-14   1
7   mary    2010-08-01    2014-06-14   1
8   mary    2010-11-01    2014-06-14   1
9   mary    2011-01-01    2014-06-14   1
10  john    2010-03-27    2011-03-01   1
11  john    2010-07-01    2011-03-01   1
12  john    2010-11-01    2011-03-01   1
13  john    2011-02-01    2011-03-01   1

我发现这篇文章很有用 R: Count unique values by category ,但答案是作为单独的表格给出的,而不是包含在 df 中。

我也试过这个:
df$ob = ave(df$elig_end_date, df$elig_end_date, FUN=seq_along)

但这会创建一个计数,我真的只想要一个指标。

先感谢您

斯蒂芬代码的产品(这不是正确的代码 - 只是作为学习点发布)
names date_of_claim elig_end_date ob
1    tom    2010-01-01    2010-07-01  2
2    tom    2010-05-04    2010-07-01  2
3    tom    2010-06-01    2014-01-01  2
4    tom    2010-10-10    2014-01-01  2
5   mary    2010-03-01    2014-06-14  5
6   mary    2010-05-01    2014-06-14  5
7   mary    2010-08-01    2014-06-14  5
8   mary    2010-11-01    2014-06-14  5
9   mary    2011-01-01    2014-06-14  5
10  john    2010-03-27    2011-03-01  4
11  john    2010-07-01    2011-03-01  4
12  john    2010-11-01    2011-03-01  4
13  john    2011-02-01    2011-03-01  4

最佳答案

使用 ave 的另一种可能性:

df$obs <- with(df, ave(elig_end_date, names,
                       FUN = function(x) cumsum(!duplicated(x))))

#    names date_of_claim elig_end_date obs
# 1    tom    2010-01-01    2010-07-01   1
# 2    tom    2010-05-04    2010-07-01   1
# 3    tom    2010-06-01    2014-01-01   2
# 4    tom    2010-10-10    2014-01-01   2
# 5   mary    2010-03-01    2014-06-14   1
# 6   mary    2010-05-01    2014-06-14   1
# 7   mary    2010-08-01    2014-06-14   1
# 8   mary    2010-11-01    2014-06-14   1
# 9   mary    2011-01-01    2014-06-14   1
# 10  john    2010-03-27    2011-03-01   1
# 11  john    2010-07-01    2011-03-01   1
# 12  john    2010-11-01    2011-03-01   1
# 13  john    2011-02-01    2011-03-01   1

关于r - 每组唯一值的累积计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20613207/

相关文章:

php - 如何计算多维数组中所有相同的项

ios - 核心数据 : best way of checking the uniqueness of an attribute

php - 如何删除具有重复列值的子数组?

mysql - 统计今天、昨天……以及其他时间添加的行数

php - MySQL - 列组合的唯一约束 : avoiding error?

r - 在 R 中使用 plotCI 时更改参数。(向左或向右移动点)

r - 是否可以获得转换后的绘图数据? (例如点图中点的坐标、密度曲线)

r - 如何在 R 中编写带有非标准引号字符的 CSV?

Rmarkdown 演示幻灯片行情

Python:有效计算字典列表中键的唯一值的数量