【问题标题】:Is there an easier way of creating this table in kdb?有没有更简单的方法在 kdb 中创建这个表?
【发布时间】:2021-05-21 11:55:07
【问题描述】:

一年中的每一天我都想要这种布局

time       area   disease   no._of_cases
----------------------------------------
2018.01.01 Spain  Influenza 46          
2018.01.02 Spain  Influenza 46          
2018.01.03 Spain  Influenza 41          
2018.01.01 Spain  Cholera   -3          
2018.01.02 Spain  Cholera   -1          
2018.01.03 Spain  Cholera   -2          
2018.01.01 London Influenza 55          
2018.01.02 London Influenza 51          
2018.01.03 London Influenza 55          
2018.01.01 London Cholera   1           
2018.01.02 London Cholera   2           
2018.01.03 London Cholera   3           
2018.01.01 France Influenza 50          
2018.01.02 France Influenza 50          
2018.01.03 France Influenza 55          
2018.01.01 France Cholera   -1          
2018.01.02 France Cholera   -2          
2018.01.03 France Cholera   3    

所以我有两种疾病,每年每个月每个不同地区的每一天都有不同的病例数

以下是一个地区有两种疾病的例子和两个月的病例数结果

Spain - Influenza
No.of cases in JAN:{floor [1339 % 30]+first 1?-3+til 7} each til 30 
No.of cases in FEB:{floor [1261 % 28]+first 1?-3+til 7} each til 28 

Spain - Cholera
No.of cases in JAN:{floor [0 % 30]+first 1?-3+til 7} each til 30;
No.of cases in FEB:{floor [0 % 28]+first 1?-3+til 7} each til 28;

这是一月份的示例

times:2020.01.01 + til 3;
area:`Spain
disease1:`Influenza
disease2:`Cholera
no.of_cases1:{floor [1339 % 30]+first 1?-3+til 7} each til 30; 
no.of_cases2:{floor [0 % 30]+first 1?-3+til 7} each til 30;

area2:`London
no.of_cases3:{floor [1583 % 30]+first 1?-3+til 7} each til 30; 
no.of_cases4:{floor [0 % 30]+first 1?-3+til 7} each til 30;

area3:`France
no.of_cases5:{floor [1583 % 30]+first 1?-3+til 7} each til 30; 
no.of_cases6:{floor [0 % 30]+first 1?-3+til 7} each til 30;


a:([]time:times;area:(count [times])# area;disease:count[times]#disease1;no._of_cases:count[times]# no.of_cases1)
b:([]time:times;area:(count [times])# area;disease:count[times]#disease2;no._of_cases:count[times]# no.of_cases2)
c:([]time:times;area:(count [times])# area2;disease:count[times]#disease1;no._of_cases:count[times]# no.of_cases3)
d:([]time:times;area:(count [times])# area2;disease:count[times]#disease2;no._of_cases:count[times]# no.of_cases4)
e:([]time:times;area:(count [times])# area3;disease:count[times]#disease1;no._of_cases:count[times]# no.of_cases5)
f:([]time:times;area:(count [times])# area3;disease:count[times]#disease2;no._of_cases:count[times]# no.of_cases6)

t:a,b,c,d,e,f

有没有更简单的方法可以为一年中的每一天创建此表?

这是我要转换为 kdb 的表的示例。

             area        |Spain         |London              |France  
____________________________________________________________________-   
disease     |Month/year |2017  2018 ... | 2017   2018  ...   |2017 2018 ...
----------------------------------------------------------------------
Influenza   |Jan        |1     2        |                    |
            |Feb        |2     3        |                    |
            |....       |...
            |
--------------------------------------------------------------------            
Cholera     |Jan        |2      9
            |Feb        |1      2
            |...

【问题讨论】:

  • 您好,您所构建的基础数据是否具有您想要平均值的每月总计?可能最好从那而不是所有这些单独的步骤中构建一些东西?你能显示这个的基础表吗?西班牙的1339和1261流感病例来自哪里?
  • 是的,它有每月的总数,我正在以此为基础生成平均值。
  • 我建议将该表读入 kdb 并尝试对其进行操作以达到您想要的结果,而不是从头开始构建表。

标签: join kdb


【解决方案1】:

如果你纯粹是在谈论如何制作桌子,你可以使用cross

q)update cases:count[i]?100 from cross/[(([]area:`Spain`London`France);([]disease:`Influenza`Cholera);([]time:2018.01.01+til 3))]
area   disease   time       cases
---------------------------------
Spain  Influenza 2018.01.01 2
Spain  Influenza 2018.01.02 39
Spain  Influenza 2018.01.03 64
Spain  Cholera   2018.01.01 49
...

但是,您的 cases 列可能需要更多的逻辑,具体取决于您的用例

【讨论】:

  • 查看问题底部 - 我已对其进行编辑,以 kdb 形式显示我想要的表格,我还想生成每日数据来代替每月。
猜你喜欢
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多