首页 > 解决方案 > 如何减去R中相同列表的列表元素?

问题描述

我有一个这样的列表:

anteilSelbststaendige <- ((100/input$bevoelkerung) * input$selbststaendige)

anteilSelbststaendige
 [1] 5.460547 5.551961 5.561330 5.474761 5.460763 5.455431 5.467829 5.409999 5.356277 5.159937 4.990305 4.915012 4.882196
[14] 4.860807 4.855824 4.826342 4.768927 4.719400 4.660732 4.615460 4.527033 4.486686 4.455891 4.908780 4.964310 4.973149
[27] 5.011707 5.050273 5.016059 5.000409 4.976963 5.014764 5.064687 5.122957 5.163057 5.284785 5.368078 5.566300 5.816041
[40] 6.028620 6.257261 6.434038 6.676781 7.157343

我想知道该值与以下值之间的区别。(对于列表的所有值)像这样:

anteilSelbststaendigeV <- anteilSelbststaendige[2] - anteilSelbststaendige[1]

结果应该是以下值的差异列表。

我尝试了“for”循环,但它不起作用,我不知道为什么。我是 R 新手,我真的不知道有什么功能和做什么。

标签: rlistfor-loopcompare

解决方案


我们可以diff用来获取当前值和下一个值之间的差异

anteilSelbststaendigeV <-  c(NA, diff(anteilSelbststaendig))

注意:根据显示的数据结构,它是 avector而不是 alist


推荐阅读