【问题标题】:Find the observations with the largest values that make up a specific percentage of the total查找具有最大值且占总数特定百分比的观测值
【发布时间】:2020-11-11 08:26:35
【问题描述】:

我观察了许多客户(或“用户”)以及他们在很多天的访问时间。现在我想选择占客户总访问时间 80% 的客户。更准确地说,我想依次选择总访问时间最高的客户,直到达到客户累计访问时间的 80% 的截止点。

示例数据:

da = data.frame(userid = c(1,1,2,2,3,3,4,4,5,5), day = c(1,2,1,2,1,2,1,2,1,2), total_visit_time = c(28,4,312,22,1,93,30,67,211,81))
da
   userid day total_visit_time
1       1   1               28
2       1   2                4
3       2   1              312
4       2   2               22
5       3   1                1
6       3   2               93
7       4   1               30
8       4   2               67
9       5   1              211
10      5   2               81

我已经弄清楚如果我对每个客户只有一次观察以及几天内的总访问时间,它是如何工作的,但我想保留我的示例数据的数据结构。

示例数据:假设我们观察到 10 个用户及其总访问时间(以秒为单位):

da = data.frame(userid = c(1,2,3,4,5,6,7,8,9,10), total_visit_time = c(28,4,312,22,1,93,30,67,211,81))
da
   userid total_visit_time
1       1               28
2       2                4
3       3              312
4       4               22
5       5                1
6       6               93
7       7               30
8       8               67
9       9              211
10     10               81

我们计算了客户的总体访问时间,即 849 秒。我们根据客户的访问时间按降序对客户进行排序:

da = da %>% mutate(overall_time = sum(total_visit_time)) %>%
   arrange(desc(total_visit_time))
da
   userid total_visit_time overall_time
1       3              312          849
2       9              211          849
3       6               93          849
4      10               81          849
5       8               67          849
6       7               30          849
7       1               28          849
8       4               22          849
9       2                4          849
10      5                1          849

我们计算客户的累计时间(按降序排列):

da = da %>%
   mutate(cumulative_time = cumsum(total_visit_time))
da
   userid total_visit_time overall_time cumulative_time
1       3              312          849             312
2       9              211          849             523
3       6               93          849             616
4      10               81          849             697
5       8               67          849             764
6       7               30          849             794
7       1               28          849             822
8       4               22          849             844
9       2                4          849             848
10      5                1          849             849

我们计算客户总时间的累计访问时间百分比:

da = da %>% mutate(percentage_of_time = cumulative_time/overall_time)
da         
   userid total_visit_time overall_time cumulative_time percentage_of_time
1       3              312          849             312              0.367
2       9              211          849             523              0.616
3       6               93          849             616              0.726
4      10               81          849             697              0.821
5       8               67          849             764              0.900
6       7               30          849             794              0.935
7       1               28          849             822              0.968
8       4               22          849             844              0.994
9       2                4          849             848              0.999
10      5                1          849             849              1.000

我们过滤掉累计访问时间百分比

da = filter(da, percentage_of_time < .80)
da
  userid total_visit_time overall_time cumulative_time percentage_of_time
1      3              312          849             312              0.367
2      9              211          849             523              0.616
3      6               93          849             616              0.726

如果我想保留对客户的所有观察结果,它如何处理我的分组数据?

【问题讨论】:

    标签: r filter dplyr panel-data


    【解决方案1】:

    这是data.table的解决方案:

    da = data.table(userid = c(1,1,2,2,3,3,4,4,5,5), 
                    day = c(1,2,1,2,1,2,1,2,1,2), 
                    total_visit_time = c(28,4,312,22,1,93,30,67,211,81))
    da[,perc_visit := sum(total_visit_time)/sum(da$total_visit_time), by = userid]
    da_unique <- unique(da[,.(userid,perc_visit)])[order(-perc_visit),][,cum_perc_visit := cumsum(perc_visit)]
    da <- merge(da,da_unique[,.(userid,cum_perc_visit)], by = "userid")[order(cum_perc_visit)]
    da[cum_perc_visit<0.8,]
    

    【讨论】:

    • 谢谢,这行得通!我通常更喜欢 dplyr,但我会在我的代码中使用它。
    猜你喜欢
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多