【问题标题】:calculate moving three-year average of grouped rows in R计算 R 中分组行的三年移动平均值
【发布时间】:2018-07-07 18:10:21
【问题描述】:

我有一个大型数据集,我正在计算大量汇总统计数据,按物种和年份分组。下面是一些用于设置数据框的玩具代码:

species <- rep(c("Farfantepenaeus duorarum", "Menticirrhus littoralis",  "Ovalipes stephensoni", "Lolliguncula brevis", "Larimus fasciatus"), 4)
years <- rep(c(2007, 2013, 2001, 2013, 1994), 4)
lat <-c(33.9085, 34.6205, 33.7895, 33.8015, 29.9625, 35.1655, 34.7950, 29.5620, 32.8960, 32.2590, 33.1320, 32.9850, 34.6605, 34.0425, 32.8360, 32.6270, 32.0680, 31.7900, 34.1960, 30.7830)
testdf <- data.frame(species, years, lat) 

我要计算的第一个统计数据是每年发现每个物种的最高 3 个纬度的平均值。我用下面的代码强行强制了这个,后来我又加入了主df:

testtop3lat <- testdf %>%
  group_by(species, years) %>%
  top_n(3, lat) %>%
  mutate(top3lat = mean(lat)) 

下一个任务是我卡住的地方。对于物种和年份的每种组合,我想计算所覆盖距离(纬度)的移动三年平均值。因此,对于每个speciesyears 组合,我想计算[(latitude at year + 1) - (latitude at year - 1)] / 3,并将其作为一列添加回来。最终,我希望主 df 中的每个观察都具有 top3lattop3slope 列,每个 speciesyears 组合具有相同的条目。

我一直在搞乱mutate 并编写自定义函数以映射到原始数据集,但到目前为止都没有工作。建议将不胜感激!

编辑抱歉,这不是最有用的玩具数据集。对一个物种进行以下观察:

obsyears <- c(1980, 1980, 1980, 1981, 1981, 1981, 1982, 1982, 1982)
obslats <- c(38.5, 37, 39.2, 41.7, 40, 38.6, 41.2, 39.8, 38.7)

在 1981 年,期望的输出是 top3lat=40.1(41.7、40 和 38.6 的平均值)作为 1981 年该物种所有行条目的列

second 1981 年所需的输出是 top3slope = [(top3lat[1982]-top3lat[1980])/3](是的,我知道这是不正确的 R 语言),这里是 (39.9-38.23)/3 = 0.56也作为所有行条目的列对于 1981 年的那个物种

【问题讨论】:

  • 预期输出是什么?

标签: r dplyr


【解决方案1】:

如果我正确理解您的要求,您应该能够在 left_joinleadlag 中实现您所追求的目标,所有这些都来自

newdf <- testdf %>%
  left_join(
    testdf %>%
      group_by(species, years) %>%
      top_n(3, lat) %>%
      mutate(
        top3lat = mean(lat),
        top3slope = (lead(lat) - lag(lat)) / 3
      ) %>%
      na.omit %>%
      select(-lat),
    by = c("species", "years")
  ) %>%
  arrange(species, years, desc(lat))

newdf    
#                     species years     lat  top3lat  top3slope
# 1  Farfantepenaeus duorarum  2007 35.1655 34.06867 -0.2588333
# 2  Farfantepenaeus duorarum  2007 33.9085 34.06867 -0.2588333
# 3  Farfantepenaeus duorarum  2007 33.1320 34.06867 -0.2588333
# 4  Farfantepenaeus duorarum  2007 32.6270 34.06867 -0.2588333
# 5         Larimus fasciatus  1994 32.8360 31.95933 -0.4920000
# 6         Larimus fasciatus  1994 32.2590 31.95933 -0.4920000
# 7         Larimus fasciatus  1994 30.7830 31.95933 -0.4920000
# 8         Larimus fasciatus  1994 29.9625 31.95933 -0.4920000
# 9       Lolliguncula brevis  2013 34.1960 34.01333  0.1315000
# 10      Lolliguncula brevis  2013 34.0425 34.01333  0.1315000
# 11      Lolliguncula brevis  2013 33.8015 34.01333  0.1315000
# 12      Lolliguncula brevis  2013 32.8960 34.01333  0.1315000
# 13  Menticirrhus littoralis  2013 34.7950 34.13350 -0.5451667
# 14  Menticirrhus littoralis  2013 34.6205 34.13350 -0.5451667
# 15  Menticirrhus littoralis  2013 32.9850 34.13350 -0.5451667
# 16  Menticirrhus littoralis  2013 32.0680 34.13350 -0.5451667
# 17     Ovalipes stephensoni  2001 34.6605 33.41333 -0.6665000
# 18     Ovalipes stephensoni  2001 33.7895 33.41333 -0.6665000
# 19     Ovalipes stephensoni  2001 31.7900 33.41333 -0.6665000
# 20     Ovalipes stephensoni  2001 29.5620 33.41333 -0.6665000

如果删除lat,这可以进一步总结

newdf %>%
  select(-lat) %>%
  distinct

#                    species years  top3lat  top3slope
# 1 Farfantepenaeus duorarum  2007 34.06867 -0.2588333
# 2        Larimus fasciatus  1994 31.95933 -0.4920000
# 3      Lolliguncula brevis  2013 34.01333  0.1315000
# 4  Menticirrhus littoralis  2013 34.13350 -0.5451667
# 5     Ovalipes stephensoni  2001 33.41333 -0.6665000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 2020-02-04
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 2016-05-16
    相关资源
    最近更新 更多