【问题标题】:How to do random sampling based on customer ID [closed]如何根据客户 ID 进行随机抽样 [关闭]
【发布时间】:2018-09-26 06:26:26
【问题描述】:

我有如下数据集

Cust_ID 子部门日期 Bill_Value 1513 洗涤剂条 20180204 30 1513 薯条 20180803 160 1513 果汁 20180204 30 1513 集装箱 20180803 79 1513 糖果和棒棒糖 20180803 165 1513 奶酪 20180204 203.04 1513 清洁剂和雨刷 20180803 159 1513 巧克力 20180803 448 1513 冷冻蔬菜零食 20180803 570 1860 咸饼干 20180110 40 1860 燕麦 20180705 60 1860 糕点 20180815 198 1860 奶酪 20180110 85 1860 素汤粉 20180814 20 1860 芝士 20180123 99 1860 北印度混合 20180502 59 1860风味牛奶20180502 40

sample data set

我想根据客户 ID 对数据进行随机抽样,以便选择所选客户的所有交易

【问题讨论】:

  • 我试图在 R 中做,预期的输出是当我做随机样本时,它应该选择客户完成的所有交易。如果不应该错过所选 Cust_ID 的任何交易
  • 你能发布预期的输出吗?举个例子会更清楚。
  • Cust_ID SubDepartment Date Bill_Value 1513 Detergent Bars 20180204 30 1513 French Fries 20180803 160 1513 Fruit Based Juices 20180204 30 1513 Containers 20180803 79 1513 Candies & Lollypops 20180803 165 1513 Cheese 20180204 203.04 1513 Cleaners & Wipers 20180803 159 1513 Chocolates 20180803 448 1513 冷冻蔬菜零食 20180803 570。随机抽样不应留下客户在不同日期/产品上完成的任何交易
  • 请在您的问题中添加“一种可能的”结果。

标签: r random sampling


【解决方案1】:

这是一个简单的解决方案,您首先获取所有唯一 ID,对其进行采样并根据示例对数据框进行子集化:

df[df$Cust_ID %in% sample(unique(df$Cust_ID), n),]

其中n 是要采样的id 的数量。

使用您的数据:

 set.seed(1) #to be reproducible  
 df[df$Cust_ID %in% sample(unique(df$Cust_ID), 1),]

输出

  Cust_ID       SubDepartment     Date Bill_Value
1    1513      Detergent.Bars 20180204      30.00
2    1513        French.Fries 20180803     160.00
3    1513  Fruit.Based.Juices 20180204      30.00
4    1513          Containers 20180803      79.00
5    1513 Candies.&.Lollypops 20180803     165.00
6    1513              Cheese 20180204     203.04
7    1513   Cleaners.&.Wipers 20180803     159.00
8    1513          Chocolates 20180803     448.00
9    1513   Frozen.Veg.Snacks 20180803     570.00

数据:

 df <- read.table(text = "Cust_ID SubDepartment       Date        Bill_Value
1513    Detergent.Bars      20180204    30
           1513    French.Fries        20180803    160
           1513    Fruit.Based.Juices  20180204    30
           1513    Containers          20180803    79
           1513    Candies.&.Lollypops 20180803    165
           1513    Cheese              20180204    203.04
           1513    Cleaners.&.Wipers   20180803    159
           1513    Chocolates          20180803    448
           1513    Frozen.Veg.Snacks   20180803    570
           1860    Salted.Biscuits     20180110    40
           1860    Oats                20180705    60
           1860    Pastries            20180815    198
           1860    Cheese              20180110    85
           1860    Veg.Powdered.Soups  20180814    20
           1860    Paneer              20180123    99
           1860    North.Indian.Mixes  20180502    59
           1860    Flavoured.Milk      20180502    40", header = TRUE)

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 2018-10-07
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    相关资源
    最近更新 更多