如何创建一个函数或循环,该函数或循环可以从每行的另一列上方总结一定量的行

发布于 2025-02-13 05:36:27 字数 552 浏览 0 评论 0原文

我正在尝试计算海洋中的学位加热周,并且想知道如何创建一个循环或其他一些可以计算此值并将其放入每行的新列(DHW)中的方法。 DHW是每天计算的值。

它是在另一列值(热点)的12周滚动窗口(84天)上计算出来的。 DHW是这84行的总和(在其自己的行中值(日)以及上面的83个),然后除以7

”

下面是我的数据框架的一个示例,我只是使用热点列来计算DHW

”输入图像在这里说明“

I am trying to calculate degree heating weeks in the Ocean and was wondering how I could create a loop or some other methods that would calculate this value and put it in a new column(DHW) for each row. DHW is a value calculated for each day.

It is calculated over a 12-week rolling window (84 days) from another column of values (hotspot). DHW is the sum of those 84 rows (value in its own row(day) plus the 83 above it) and then divided by 7

enter image description here

Below is an example of my data frame, I am just using the Hotspot column to calculate DHW

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气尐潴 2025-02-20 05:36:27

如果我理解正确地

修复了width = 3 to width = 84

df <-
  data.frame(DAYS = seq(as.Date("2022-01-01"), length.out = 10,  by = "1 days"),
             HS = seq(10))

library(dplyr)

df %>%
  mutate(DWH = zoo::rollapply(
    HS,
    width = 3,
    FUN = function(x) sum(x) / 7,
    fill = NA,
    align = "right"
  ))
#>          DAYS HS       DWH
#> 1  2022-01-01  1        NA
#> 2  2022-01-02  2        NA
#> 3  2022-01-03  3 0.8571429
#> 4  2022-01-04  4 1.2857143
#> 5  2022-01-05  5 1.7142857
#> 6  2022-01-06  6 2.1428571
#> 7  2022-01-07  7 2.5714286
#> 8  2022-01-08  8 3.0000000
#> 9  2022-01-09  9 3.4285714
#> 10 2022-01-10 10 3.8571429

在2022-07-04创建的

if I understood correctly

fix width = 3 to width = 84

df <-
  data.frame(DAYS = seq(as.Date("2022-01-01"), length.out = 10,  by = "1 days"),
             HS = seq(10))

library(dplyr)

df %>%
  mutate(DWH = zoo::rollapply(
    HS,
    width = 3,
    FUN = function(x) sum(x) / 7,
    fill = NA,
    align = "right"
  ))
#>          DAYS HS       DWH
#> 1  2022-01-01  1        NA
#> 2  2022-01-02  2        NA
#> 3  2022-01-03  3 0.8571429
#> 4  2022-01-04  4 1.2857143
#> 5  2022-01-05  5 1.7142857
#> 6  2022-01-06  6 2.1428571
#> 7  2022-01-07  7 2.5714286
#> 8  2022-01-08  8 3.0000000
#> 9  2022-01-09  9 3.4285714
#> 10 2022-01-10 10 3.8571429

Created on 2022-07-04 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文