使用 Excel 显示日期范围内出现的次数

发布于 2024-08-18 08:26:20 字数 345 浏览 2 评论 0原文

我有一个交易日期列表以及在该日期进行交易的人的用户 ID(仅允许 1 Tx/天)。例如:

我想创建一个矩阵,显示截至每个日期的用户数量1笔交易、2-10笔交易、10-20笔交易等。例如(注意,以下数据与上面的交易数据不对应):

数据透视表是我最好的机制吗?如果是这样(或不是)我将如何处理这个问题?

I've got a list of transaction dates and the user id of the person who made the transaction on that date (just 1 Tx/day allowed). For example:

I'd like to create a matrix which shows, as of each date, the number of users who have made 1 transaction, 2-10 transactions, 10-20 transactions, etc. For example (note, the below data doesn't correspond to the transaction data above):

Is a pivot table my best mechanism here? If so (or not) how would I approach this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羅雙樹 2024-08-25 08:26:20

我的投票使用枢轴
如果您有 2007 年类似的

数据 1) 选择您上面的数据
2) 插入枢轴
3) 将日期拖至行 Loabel
4) 将用户 ID 拖至列 =>每个用户 ID 获得一列
5)在值中,youiu 应该有用户数
6) 然后您需要添加新列来计算分段 1-10 等中的用户数量

My vote use a pivot
If you have 2007 something like this

1) Select the data you have above
2) Do Insert Pivot
3) Drag Date to Row Loabel
4) Drag User ID to Columns => you get one column per user ID
5) In Values yoiu should have Count of Users
6) Then you need to add new columns that calculates the number of users that are in segment 1-10 etc

土豪我们做朋友吧 2024-08-25 08:26:20

我知道我要说的有点“超出范围”,但我遇到了这样的问题,我使用了 R 来解决它。 (如果我没有使用 R,我想我会尝试 sql,但我绝不会选择 excel)

我还有一个名为“trans_data”的 2 列表,就像你的一样。列名称为“trans_date”和“user_id”。我还想要一个像您一样的列联表,其中包含特定交易限制内的用户数量。

这是代码

library(plyr)
adply(table(trans_date),1,function(x) {
     d = NULL
     d["1"] <- sum(x==1)
     d["2_to_5"] <- sum(x > 1 & x <= 5) 
     d["6_to_27"] <- sum(x > 5 & x <= 27)
     d["28_to_120"] <- sum(x > 27 & x <= 120)
     d["121_to_398"] <- sum(x > 120 & x <= 398)
     d[">_398"] <- sum(x > 398)
     return(d)
   }
)

和部分结果

  trans_date   1 2_to_5 6_to_27 28_to_120 121_to_398 >_398
1 2009-01-25 257    169      61         7          1     0
2 2009-01-26 145    125      53         3          1     0
3 2009-01-27 175    117      44        12          0     0
4 2009-01-28 171    138      49         7          4     0
5 2009-01-29 756    217      71         5          3     0

I know what I am going to say is a bit "out of scope", but I had a problem like this and I used R to work around it instead. (If I hadn't use R, I think I would have tried sql but in no way I would choose excel)

I also have a 2-columns table named "trans_data", like yours. The column names are "trans_date" and "user_id". I also wanted a contingency table like yours with counts of users within specific transaction limits.

Here is the code

library(plyr)
adply(table(trans_date),1,function(x) {
     d = NULL
     d["1"] <- sum(x==1)
     d["2_to_5"] <- sum(x > 1 & x <= 5) 
     d["6_to_27"] <- sum(x > 5 & x <= 27)
     d["28_to_120"] <- sum(x > 27 & x <= 120)
     d["121_to_398"] <- sum(x > 120 & x <= 398)
     d[">_398"] <- sum(x > 398)
     return(d)
   }
)

and part of the result

  trans_date   1 2_to_5 6_to_27 28_to_120 121_to_398 >_398
1 2009-01-25 257    169      61         7          1     0
2 2009-01-26 145    125      53         3          1     0
3 2009-01-27 175    117      44        12          0     0
4 2009-01-28 171    138      49         7          4     0
5 2009-01-29 756    217      71         5          3     0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文