r - 如何减去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 新手,我真的不知道有什么功能和做什么。
解决方案
我们可以diff
用来获取当前值和下一个值之间的差异
anteilSelbststaendigeV <- c(NA, diff(anteilSelbststaendig))
注意:根据显示的数据结构,它是 avector
而不是 alist
推荐阅读
- c# - CultureInfo.InstalledUICulture vs Thread.CurrentThread.CurrentUICulture vs regional settings
- r - 在一张图中绘制多个密度图
- python - 使用 schedule 时如何获取返回值
- php - 如何使用 ajax 数据表以模式形式显示数据库内容
- boolean - 这种逻辑关系正确吗?
- oracle - 适用于 AWS 联合 Athena 的 Oracle JDBC 连接
- r - 使用 knit 按钮将一个 markdown 文件编织成两个输出文件
- ios - 候选人具有“明显匹配类型”的非匹配类型
- react-native - 样式单选按钮
- azure-devops - 如何同步 Azure DevOps Pipeline Stage 的运行?