使用R中的循环函数来计算沉降速率?

时间:2019-10-02 20:19:32

标签: r

我有一些河流沉积数据。每条河流由2个站点组成,带有2-3个沉积板,进行了10年的实验,并以1-3年的间隔测量了每个板上沉积的沉积物的沉积深度(mm)。我想知道是否可以使用R回路来计算每条河流每个站点的沉积率?附有样本数据。

River	Site	Plate	Year	"Sedement 
depth(mm)"
ES1	SiteA	Plate 1	2009	220
ES1	SiteA	Plate 1	2011	207
ES1	SiteA	Plate 1	2012	203
ES1	SiteA	Plate 1	2013	210
ES1	SiteA	Plate 1	2015	211.7
ES1	SiteA	Plate 1	2016	218
ES1	SiteA	Plate 1	2018	207
ES1	SiteA	Plate 1	2019	219.7
ES1	SiteA	Plate 2	2009	183
ES1	SiteA	Plate 2	2011	173
ES1	SiteA	Plate 2	2012	180
ES1	SiteA	Plate 2	2013	180
ES1	SiteA	Plate 2	2015	184
ES1	SiteA	Plate 2	2016	185
ES1	SiteA	Plate 2	2018	178
ES1	SiteA	Plate 2	2019	185.3
ES2	SiteB	Plate 1	2009	191
ES2	SiteB	Plate 1	2011	190
ES2	SiteB	Plate 1	2012	190
ES2	SiteB	Plate 1	2013	191
ES2	SiteB	Plate 1	2015	192.3
ES2	SiteB	Plate 1	2016	195
ES2	SiteB	Plate 1	2018	186
ES2	SiteB	Plate 1	2019	190.3
ES2	SiteB	Plate 2	2009	212
ES2	SiteB	Plate 2	2011	214
ES2	SiteB	Plate 2	2012	205
ES2	SiteB	Plate 2	2013	203
ES2	SiteB	Plate 2	2015	190.7
ES2	SiteB	Plate 2	2016	197
ES2	SiteB	Plate 2	2018	188
ES2	SiteB	Plate 2	2019	191.3

每个部位的沉降率可计算如下;

例如ES1,SiteA,2015年=第1板(211.7-210)/ 2(年差)                             板2(184-180)/ 2

  Sedimentation rate for 2015 = avg [Plate 1 (211.7-210)/2(year diff)+ Plate 2 (184-180)/2]

1 个答案:

答案 0 :(得分:0)

假设您已将数据上传到R。然后,您可以执行dplyr

library(dplyr)
#create the year diff column, the rate diff column and the rate diff column
table <- table %>%
   group_by(River, Site, Plate) %>%
   mutate(Year_diff = Year - lag(Year),
          sediment_depth_diff = Sedement_depth_mm - lag(Sedement_depth_mm),
          sediment_depth_diff_per_year = sediment_depth_diff/Year_diff)

#create new table with average rate per river, site, year
table_summary <- table %>% 
    ungroup() %>%
    group_by(River, Site, Year) %>%
    summarise(avg_rate = mean(sediment_depth_diff_per_year))