按组计算连续行中值之间的差异你好呀,
我有一个包含 2 列的数据表,如下所示:
group value
<SPAN class="hljs-number">1</SPAN> <SPAN class="hljs-number">10</SPAN>
<SPAN class="hljs-number">1</SPAN> <SPAN class="hljs-number">20</SPAN>
<SPAN class="hljs-number">1</SPAN> <SPAN class="hljs-number">25</SPAN>
<SPAN class="hljs-number">2</SPAN> <SPAN class="hljs-number">5</SPAN>
<SPAN class="hljs-number">2</SPAN> <SPAN class="hljs-number">10</SPAN>
<SPAN class="hljs-number">2</SPAN> <SPAN class="hljs-number">15</SPAN>
我想创建一个新列“diff”,它计算连续行之间的差异,按“group”分组,所以我会有这样的东西:
group value diff
<SPAN class="hljs-number">1</SPAN> <SPAN class="hljs-number">10</SPAN> . <SPAN class="hljs-comment"># because there is a no previous value</SPAN>
<SPAN class="hljs-number">1</SPAN> <SPAN class="hljs-number">20</SPAN> <SPAN class="hljs-number">10</SPAN> <SPAN class="hljs-comment"># value[2] - value[1]</SPAN>
<SPAN class="hljs-number">1</SPAN> <SPAN class="hljs-number">25</SPAN> <SPAN class="hljs-number">5</SPAN> <SPAN class="hljs-comment"># value[3] value[2]</SPAN>
<SPAN class="hljs-number">2</SPAN> <SPAN class="hljs-number">5</SPAN> . <SPAN class="hljs-comment"># because group is changed</SPAN>
<SPAN class="hljs-number">2</SPAN> <SPAN class="hljs-number">10</SPAN> <SPAN class="hljs-number">5</SPAN> <SPAN class="hljs-comment"># value[5] - value[4]</SPAN>
<SPAN class="hljs-number">2</SPAN> <SPAN class="hljs-number">15</SPAN> <SPAN class="hljs-number">5</SPAN> <SPAN class="hljs-comment"># value[6] - value[5]</SPAN>
</CODE><CODE class="hljs language-r">
你能帮我处理列公式的 jsl 吗?
非常感谢,
艾丽西亚
数据表脚本回复:按组计算连续行中值之间的差异完美运行! 非常感谢
回复:按组计算连续行中值之间的差异如果表格总是像您的示例一样按组排序,您可以使用 if 语句执行此操作:
If( Lag( :group ) == :group,
:value - Lag( :value ),
.
)