计算两个值之间的观察数

时间:2018-06-08 22:54:20

标签: r

对于每个单独的stormMark值,我想计算自流列中最后一个非零数字以来的天数。以下是我的数据示例:

> head(newdat)
        dates  station flow stormMark
 1 2008-01-01 09512162  20        1
 2 2008-01-02 09512162   0       NA
 3 2008-01-03 09512162   5       NA
 4 2008-01-04 09512162   0       NA
 5 2008-01-05 09512162   0       NA
 6 2008-01-06 09512162 3.5        2

所需的输出看起来像这样:

stormMark days.since.flow
 1          NA
 2          3

1 个答案:

答案 0 :(得分:0)

使用import IPython.core.magic as ipym from IPython import get_ipython @ipym.magics_class class MareBearMagics(ipym.Magics): @ipym.line_magic def gvim(self, line): cell_text = """ IPython.keyboard_manager.command_shortcuts.add_shortcut('g', { handler : function (event) { var input = IPython.notebook.get_selected_cell().get_text(); var cmd = "f = open('.toto.py', 'w');f.close()"; if (input != "") { cmd = '%%writefile .toto.py\\n' + input; } IPython.notebook.kernel.execute(cmd); cmd = "import os;os.system('gvim .toto.py')"; IPython.notebook.kernel.execute(cmd); return false; }} ); IPython.keyboard_manager.command_shortcuts.add_shortcut('u', { handler : function (event) { function handle_output(msg) { var ret = msg.content.text; IPython.notebook.get_selected_cell().set_text(ret); } var callback = {'output': handle_output}; var cmd = "f = open('.toto.py', 'r');print(f.read())"; IPython.notebook.kernel.execute(cmd, {iopub: callback}, {silent: false}); return false; }} ); """ ipython = get_ipython() ipython.run_cell_magic( magic_name='javascript', line=None, cell=cell_text) print("Cell contents can now be edited via Gvim. From command mode " "use 'g' to open current cell contents in Gvim. After ':wq' " "from Gvim, use 'u' in command mode to update cell contents.") if __name__ == '__main__': get_ipython().register_magics(MareBearMagics) %gvim%的一个解决方案可以通过在前一个tidyverse非零的情况下使用之前的difftime填充non_zero_flow_date来实现。如果之前的datesflow,则将flow设置为0。使用non_zero_flow_date填充NA列的tidyr::fill值。最后,使用NA函数获取non_zero_flow_datedates之间的差异。

non_zero_flow_date

数据:

difftime