对于每个单独的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
答案 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
来实现。如果之前的dates
为flow
,则将flow
设置为0
。使用non_zero_flow_date
填充NA
列的tidyr::fill
值。最后,使用NA
函数获取non_zero_flow_date
和dates
之间的差异。
non_zero_flow_date
数据:强>
difftime