如何在数据帧上应用 R 中的 10 年平均过滤器?

How to apply 10-year average filter in R on a dataframe?

如何运行在此数据帧上的 NBP 上进行 10 年平均过滤?

这是数据框的头部

> head(df3)
  Year         NBP
1 1850  35.454343
2 1851  4.5634543
3 1852  112.389182
4 1853  151.169251
5 1854  73.123145
6 1855  -72.309647

实际上我有从 1850 年到 2100 年的年份,我如何在变量 NBP 的这个数据帧上的 NBP 上应用 10 年平均过滤器并在时间上绘制它?

一个选项是使用 slider 包函数 slide_dbl(),它允许您创建滚动变量。这里的代码:

library(slider)
library(dplyr)
set.seed(123)
#Data
df <- data.frame(Year=1990:2020,NBP=rnorm(31,2,0.5))
# Rolling by group
df %>% 
  mutate(rollingNBP = slide_dbl(NBP, mean, .before = 9, .complete = T))

输出:

   Year       NBP rollingNBP
1  1990 1.8399718         NA
2  1991 1.3442388         NA
3  1992 1.7001958         NA
4  1993 1.9352947         NA
5  1994 2.4433681         NA
6  1995 1.9243020         NA
7  1996 2.1648956         NA
8  1997 0.3863386         NA
9  1998 1.6141041         NA
10 1999 2.1432743   1.749598
11 2000 1.3897440   1.704576
12 2001 2.2172752   1.791879
13 2002 2.4000884   1.861868
14 2003 1.9180345   1.860142
15 2004 2.6214594   1.877952
16 2005 1.5328075   1.838802
17 2006 2.1968543   1.841998
18 2007 2.2018157   2.023546
19 2008 1.5567816   2.017813
20 2009 1.3405312   1.937539
21 2010 2.0144220   2.000007
22 2011 1.7839351   1.956673
23 2012 2.8449363   2.001158
24 2013 2.6141964   2.070774
25 2014 2.1380117   2.022429
26 2015 1.4755122   2.016700
27 2016 1.7395653   1.970971
28 2017 2.8116013   2.031949
29 2018 1.4649659   2.022768
30 2019 2.8429436   2.173009
31 2020 1.8791551   2.159482

如果要包含情节,可以使用ggplot2:

library(ggplot2)
#Code2
df %>% 
  mutate(rollingNBP = slide_dbl(NBP, mean, .before = 9, .complete = T)) %>%
  ggplot(aes(x=Year,y=rollingNBP))+
  geom_line()

输出:

如果你想看这两个系列,试试这个:

library(tidyr)
#Code 3
df %>% 
  mutate(rollingNBP = slide_dbl(NBP, mean, .before = 9, .complete = F)) %>%
  pivot_longer(-Year) %>%
  ggplot(aes(x=Year,y=value,group=name,color=name))+
  geom_line()

输出:

rollmean 来自 zoo

的选项
library(dplyr)
library(zoo)
df %>%
    mutate(rollingNBP = rollmeanr(NBP, k = 10, fill = NA))